O
O
on_click2020-12-24 11:27:46
Laravel
on_click, 2020-12-24 11:27:46

Why doesn't lighthouse return data with this condition in the model?

Hello!
There are 3 models: Course, Section, Lesson
They are consecutively "nested" into each other, and are connected via ->hasMany

CourseModel
public function sections() {
    return $this->hasMany(Section::class);
}

SectionModel
public function lessons() {
    $current_id = $this->getKey('course_id');
    return $this->hasMany(Lesson::class, 'section_id', 'section_id')->where('course_id', $current_id);
}


With such a request from the controller, the data is returned as expected (I request course, I get section in it, and I already get the lesson list from it)
$course = Course::find(1);
$sections = $course->sections;
foreach($sections as $section) {
    $lessons = $section->lessons->toArray();
    dump($lessons);
}

5fe44dbb636c6952267773.jpeg

The problem with the lighthouse graphql, if I don’t limit the selection in the section model to this piece , the query is processed, but obviously it returns too much and with it, nothing gets into the lesson list, is it possible to somehow fix this without resorting to a custom query? Thank you!
->where('course_id', $current_id)

5fe450d3e3dc6267790986.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
on_click, 2020-12-24
@on_click

I found a library that completely copes with my task - https://github.com/topclaudy/compoships
I also found the answer to my question there:

Trying to use where clauses (like in the example below) won't work when eager loading the relationship because at the time the relationship is processed $this->team_id is null.

Perhaps, nevertheless, someone knows an alternative solution (without using libraries)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question