Laravel Join Query (Left Join, Inner Join, Right Join, Cross Join) Example
In this tutorial i will discuss about laravel left join multiple conditions. I will show you how we can write laravel left join query. Using this article you can learn how to join laravel join two tables.
Sometimes we need to join multiple table in Laravel. Suppose we need to join product table with category. In laravel we can do it in two ways. One is eloquent relationship and other is join with laravel query builder.
So by this following laravel left join tutorial you will learn how to display data from two tables in laravel. So let's start out Laravel left join example tutorial.
Inner Join Clause
The query builder sometimes may also be used to write join statements. To perform a simple basic "inner join", you should use the
join method on a query builder instance.
$users = DB::table('users') ->join('contacts', 'users.id', '=', 'contacts.user_id') ->join('orders', 'users.id', '=', 'orders.user_id') ->select('users.*', 'contacts.phone', 'orders.price') ->get();
Left Join / Right Join Clause
If you want to perform a "left join" or "right join" instead of an "inner join", use the
rightJoin methods. These methods have the same signature as the
$users = DB::table('users') ->leftJoin('posts', 'users.id', '=', 'posts.user_id') ->get(); $users = DB::table('users') ->rightJoin('posts', 'users.id', '=', 'posts.user_id') ->get();
Cross Join Clause
To perform a "cross join" use the
crossJoin method with the name of the table you wish to cross join to. Cross joins generate a cartesian product between the first table and the joined table:
$sizes = DB::table('sizes') ->crossJoin('colors') ->get();
Read also : How to Use groupBy() having() and havingRaw() with DB::raw in Laravel ?
Hope this laravel join tutorial can help you.