Laravel + Vue return data with more than one relation

I have a model called Exercise, that belongs to Topic, and a Topic belongs to a Subject.

With

Exercise::with('topic')->get()

I can access properties from the current topic of the exercise inside Vue. What can I do to return also the data from the subject that the topic that my exercise belongs to?

If I was able to use just blade template, I would just call:

{{$exercise->topic->subject->name}}

But the thing is that I'm passing everything as a json to work with the data in a datatable with vue.

One solution is to also pass Subject::all to my view, and use the subject_id inside the exercise>topic with vue:

@{{subjects[exercise.topic.subject_id].name}}

I'm looking for a better and cleaner solution than that. Is there a Laravel method, like multiple with's with multiple relations? Something like:

Exercise::with('topic')->and('topic')->with('subject')->get()

Thank you!

#json #laravel #vue-js

1 Likes19.95 GEEK