0

I have a model file "Student" that contains the relation :

 public function implementations()
 {
       return $this->hasMany(Implementation::class);
  }

In my Implementation model, I have theses relations :

public function score()
    {
        return $this->belongsTo(Score::class, 'id', 'implementation_id');
    }

public function project()
    {
        return $this->belongsTo(Project::class);
    }

I would like to retrieve a table with all its data.

I tried this

public function getStudents($id)
{
    $event = Event::where('id', $id)->first();
    $data = $event->students()->with('implementations')->get();

    return response()->json($data);
}

It works. But I do not have the result I would like. I would also like to recover the implementations data with theproject and score relationships

I tried that but it does not work

public function getStudents($id)
{
    $event = Event::where('id', $id)->first();
    $data = $event->students()->with('implementations')->with('project', 'score')->get();

    return response()->json($data);
}

Thank you very much for your help!

3
  • 1
    Have you tried Event::with('students', 'students.implementations') yet? You can eager load relations of relations using the dot notation. Commented Jan 21, 2019 at 18:54
  • It's work ! $data = $event->students()->with('implementations', 'implementations.project', 'implementations.score')->get(); Thank you ! Commented Jan 21, 2019 at 18:58
  • No problem. Posted as an answer for future viewers. Commented Jan 21, 2019 at 19:09

1 Answer 1

3

Have you tried Event::with('students', 'students.implementations') yet? You can eager load relations of relations using the dot notation.

Sign up to request clarification or add additional context in comments.

1 Comment

I use this relation and I am unable to use eager load relation value inside the relation $sql->with(['postable' => function($sql) use($is_detail) { if ($is_detail && in_array(config('general.post_type_index.search'), ["postable.post_type_id"])) { $sql->with(["postable"]); } }]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.