You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've found a strange behaviour when I'm trying to load a relation which is filtered. To make my issue clear, I've got the following (simplified) Quiz model, which has multiple questions in an json column, an "active_question_key", and has a relation with quizAnswers:
As you can see in the model, the activeQuestionAnswers() relation, is based on the attribute active_question_key.
If I use this relation directly on the model, I get the result as expected:
dd($quiz->activeQuestionAnswers);
// --> Collection with x amount of answers with question_key same as active_question_key
However, if use ->load() or ->loadMissing(), the collection is empty
When I debug the relation as the following, I notice when using ->load() or ->loadMissing(), the relation method is not used on the selected model, but on a new and empty model class:
public activeQuestionAnswers(): HasMany
{
dd($this->active_question_key, $this->id);
return$this->answers()->where('question_key', $this->active_question_key);
}
The dump are as follows:
$quiz->activeQuestionAnswers; // dumps "question_key" and "1"$quiz->loadCount('activeQuestionAnswers'); // dumps "question_key" and "1"$quiz->loadMissing('activeQuestionAnswers'); // dumps "null" and "null"$quiz->load('activeQuestionAnswers'); // dumps "null" and "null"
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi all,
I've found a strange behaviour when I'm trying to load a relation which is filtered. To make my issue clear, I've got the following (simplified) Quiz model, which has multiple questions in an json column, an "active_question_key", and has a relation with quizAnswers:
The data of the
$quiz->questions
attribute looks in this example something like the following:As you can see in the model, the
activeQuestionAnswers()
relation, is based on the attributeactive_question_key
.If I use this relation directly on the model, I get the result as expected:
However, if use
->load()
or->loadMissing()
, the collection is empty$quiz->load(['activeQuestionAnswers`]); dd($quiz->activeQuestionAnswers); // -> empty Collection
When I debug the relation as the following, I notice when using
->load()
or->loadMissing()
, the relation method is not used on the selected model, but on a new and empty model class:The dump are as follows:
My questions are:
Beta Was this translation helpful? Give feedback.
All reactions