Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bonesmang's avatar

Join 3 or more tables

I've seen in the laravel 5 docs that you can join 2 tables, and that seems simple enough. But is there a way to join 3 or more tables?

Specifically, I have a master tables and 3 sub tables that each have a type_id that matches the master table ID. How can I select from all 3 tables based on the master ID?

0 likes
2 replies
bobbybouwmann's avatar

You can always keep chaining the joins

Post::join('comments', 'posts.id', '=', 'comments.post_id')
    ->join('likes', 'posts.id', '=', 'likes.post_id')
    ->join('images', 'posts.id', '=', 'images.post_id')
    ->get();

However you can make it yourself much easier when you setup the relations

Post::with(['comments', 'likes', 'images'])->get();

Source: http://laravel.com/docs/5.1/eloquent-relationships

1 like
bonesmang's avatar

Excellent! Makes perfect sense. Thanks so much for the quick response.

Please or to participate in this conversation.