Skip to content

Model attributes are empty, in relationship method, over the api. #296

Open
@ahmed-osama-saad

Description

@ahmed-osama-saad

I have relationship defined on WorkoutResults model

public function unfinishedExercises()
    {
        $exercises = $this->exercises();
        $id = $this->id;

        return $exercises->whereDoesntHave('exercisesResults', function ($query) use ($id) {
            $query->where('workout_result_id', $id);
        });
    }

In WorkoutResultSchema, I define the relationship field

HasMany::make('unfinished_exercises', 'unfinishedExercises')->type('exercises'),

The issue I'm facing:

  • $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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions