Laravel 6 Joins Example Tutorial

In this Laravel 6 Tutorial, we would love to share with you, how many types of joins in laravel and how to use this joins with queries.

When we develop any application in laravel, we want to fetch records from the databse.
Sometimes, we need to fetch records more than two or n-tables at that time, we will use joins clauses in laravel.

In this article, you will learn laravel joins clauses and how to improve the performance of your laravel application.

Table of Contents

  • Inner Join Clause
  • Left Join / Right Join Clause
  • Cross Join Clause
  • Advanced Join Clauses
  • Sub-Query Joins

Inner Join Clause

Laravel Inner Join clause selects records if the given column values matching in both tables.

When joining two tables in Laravel, you should be specific about what you are choosing.

Let’s say you have two tables, users and posts. Both have ID columns, and there is a user_id in the post, which is a foreign key for the users’ table.

$user = User::join('posts', 'posts.user_id', '=', 'users.id')
       ->select('users.*')
       ->get();

Left Join / Right Join Clause

Left Join Clause

The Laravel LEFT JOIN clause returns all rows from the left table, even if there are no matches in the right table, The result is NULL from the right side.

 User::leftJoin('posts', 'posts.user_id', '=', 'users.id')
       ->select('users.*')
       ->get();

Right Join Clause

The Laravel Right JOIN clause returns all rows from the right table, even if there are no matches in the left table, The result is NULL from the left side.

 User::rightJoin('posts', 'posts.user_id', '=', 'users.id')
       ->select('users.*')
       ->get();

Cross Join Clause

The CROSS JOIN joined every row from the first table (T1) with every row from the second table (T2).

Size::crossJoin('colours')
     ->get();

Advanced Join Clauses

If you would like to use a “where” style clause on your joins, you may use the where and orWhere methods on a join. Instead of comparing two columns, these methods will compare the column against a value:

DB::table('users')
        ->join('contacts', function ($join) {
            $join->on('users.id', '=', 'contacts.user_id')
                 ->where('contacts.user_id', '>', 5);
        })
        ->get();

Sub-Query Joins

DB::table('posts')
->select('user_id', DB::raw('MAX(created_at) as last_post_created_at'))
->where('is_published', true)->groupBy('user_id');

Conclusion

In this tutorial, you have learned laravel joins like inner join, left join, right join, advanced join, cross join and sub-query join.

15 Comments

  1. WWW.XMC.PL said:

    Howdy! I simply would like to give a huge thumbs up for the great data you have right here on this post. I shall be coming back to your blog for extra soon.

    January 27, 2021
    Reply
  2. Leonora said:

    I am curious to find out what blog platform
    you happen to be using? I’m having some minor security
    issues with my latest site and I would like to find something
    more safeguarded. Do you have any suggestions?

    Feel free to surf to my blog cheap flights

    January 31, 2021
    Reply
  3. Shavonne said:

    After I initially left a comment I appear to have clicked the -Notify me when new comments are added- checkbox and now every time a
    comment is added I get four emails with the exact same comment.
    There has to be an easy method you are able to remove me from that service?
    Thanks a lot!

    Feel free to visit my web page :: cheap flights

    February 1, 2021
    Reply
  4. Eve said:

    What’s up to all, since I am truly eager of reading this blog’s post to be updated
    on a regular basis. It carries good information.

    Feel free to surf to my website; cheap flights

    February 1, 2021
    Reply
  5. Sarah said:

    hi!,I like your writing very much! proportion we keep in touch extra approximately your article on AOL?
    I need a specialist in this space to unravel my problem.
    May be that’s you! Looking forward to see you.

    Here is my web site cheap flights – http://tinyurl.com,

    February 3, 2021
    Reply
  6. Stacy said:

    WOW just what I was searching for. Came here by searching for cheap flights

    February 3, 2021
    Reply
  7. Rose said:

    Hey There. I discovered your blog using msn. This is a very well written article.
    I’ll be sure to bookmark it and come back to learn more
    of your useful info. Thanks for the post. I will definitely
    comeback.

    Have a look at my blog post – cheap flights – http://tinyurl.com/,

    February 3, 2021
    Reply
  8. https://www.yamatocosmos.com said:

    best i have ever seen !

    February 8, 2021
    Reply
  9. https://www.shinsen-mart.com said:

    This is something New !

    February 9, 2021
    Reply
  10. 온라인카지노 said:

    Thanks for discussing your ideas. I’d personally also like to state that video games have been ever before evolving. Technology advances and innovations have made it simpler to create authentic and enjoyable games. Most of these entertainment video games were not really sensible when the concept was being attempted. Just like other designs of technological innovation, video games also have had to develop as a result of many many years. This is testimony on the fast continuing development of video games.
    온라인카지노

    February 11, 2021
    Reply
  11. 온라인카지노 said:

    An additional issue is really that video gaming became one of the all-time biggest forms of excitement for people of all ages. Kids enjoy video games, and adults do, too. The particular XBox 360 is probably the favorite video games systems for individuals that love to have a lot of activities available to them, and who like to relax and play live with other individuals all over the world. Many thanks for sharing your opinions.
    온라인카지노

    February 11, 2021
    Reply
  12. 카지노사이트 said:

    Aw, this was a really good post. Taking a few minutes and actual effort to create a good article… but what can I say… I hesitate a whole lot and don’t seem to get nearly anything done.
    카지노사이트

    February 11, 2021
    Reply
  13. Stewart said:

    Hi, of course this paragraph is really nice and I have learned lot of things from it concerning blogging.
    thanks.

    Here is my page :: cheap flights (tinyurl.com)

    February 12, 2021
    Reply
  14. https://www.songmanhits.com said:

    Best view i have ever seen !

    February 24, 2021
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *