Ad

Retrieving A Many To Many To Many Relation

- 1 answer

I have a user that has (many to many) books and each book can have many tags. I want to retrieve one specific user's books that have at least one tag like this:

book1 -> tag1
      -> tag2
book2 -> tag3
      -> tag2

How do I do that?

Ad

Answer

You can chain the eager loading like:

$user->with(['books.tags'])->get();

In case you need help with setting the relationships check the documentation.

Ad
source: stackoverflow.com
Ad