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
$this in the unfinishedExercises method is an empty model.
Consequently, $this->id is null, so the filtering in the whereDoesntHave clause doesn't work as expected.
After some debugging, I found that the problem only occurs when this relationship is requested over the api.
When using the relationship in tinker, $this is defined correctly, and the filtering works as expected.
If I request this relationship from workout-results/:id?include=unfinished_exercise endpoint, the filtering doesn't happen, I get all exercises.
The text was updated successfully, but these errors were encountered:
Basically, you can't use $this->id in a relationship method, as the relationships are not always loaded from a specific model - otherwise you'd get N+1 problems.
We use eager loading, so that we don't get N+1 problems.
LaravelJsonAPI package aside, this is bad by design.
public function unfinishedExercises()
{
$exercises = $this->exercises();
$id = $this->id;
return $exercises->whereDoesntHave('exercisesResults', function ($query) use ($id) {
$query->where('workout_result_id', $id);
});
}
Should instead be changed to something along these lines:
// class Exercise
...
public function WorkoutResult(): BelongsTo
{
return $this->belongsTo(WorkoutResult::class);
}
public function ExerciseResults(): HasMany
{
return $this->hasMany(ExerciseResult::class);
}
// class WorkoutResult
...
public function exercises(): HasMany
{
return $this->hasMany(Exercise::class);
}
public function unfinishedExercises(): HasMany
{
return $this->exercises()
->whereDoesntHave('exerciseResults');
}
Without knowing your keys etc can't give a more precise answer.
I have relationship defined on WorkoutResults model
In WorkoutResultSchema, I define the relationship field
HasMany::make('unfinished_exercises', 'unfinishedExercises')->type('exercises'),
The issue I'm facing:
After some debugging, I found that the problem only occurs when this relationship is requested over the api.
When using the relationship in tinker,
$this
is defined correctly, and the filtering works as expected.If I request this relationship from
workout-results/:id?include=unfinished_exercise
endpoint, the filtering doesn't happen, I get all exercises.The text was updated successfully, but these errors were encountered: