In this tutorial you will learn about the Laravel 7 Eloquent Join Example Tutorial and its application with practical example.
In this Laravel 7 eloquent join example tutorial I will show you how many types of join and subquery join in laravel you will also learn to use laravel join with eloquent queries. In this tutorial you will learn about various joins in laravel eloquent model. In this article I’ll demonstrate the use of various types of laravel joins.In this step by step laravel join tutorial I will share example of laravel eloquent join (join, left join, right join, cross join, and advance join) and subquery join.
Laravel 7 Eloquent Join Example Tutorial
In this Laravel 7 Eloquent Join Example Tutorial we will learn about following laravel eloquent join query with examples:
- Laravel Join Eloquent
- Laravel Left Join / Right Join Eloquent
- Laravel Cross Join Eloquent
- Laravel Advanced Join Eloquent
- Sub-Query Joins
In laravel model when you want to fetch data from multiple table with associated relationship In that case you can fetch data using laravel joins. In laravel as per the need we have to use left join(), right join(), cross join() or to join two or multiple table.
1: Laravel Join Eloquent
In laravel if we join two tables it selects records if the given column values matching in both tables. It’s also known as laravel inner join eloquent. 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.’
1 2 3 |
$user = User::join('posts', 'posts.user_id', '=', 'users.id') ->select('users.*') ->get(); |
2: Laravel Left Join / Right Join Eloquent
Left Join Eloquent
Laravel left join query returns all rows from the left table, even if there is no matches in the right table. When you are using laravel left join eloquent with a query builder laravel left join fetch data from the left table. Even if there is no match in the right table.
1 2 3 |
User::leftJoin('posts', 'posts.user_id', '=', 'users.id') ->select('users.*') ->get(); |
Right Join Eloquent
Laravel right join query returns all rows from the right table, even if there is no matches in the left table. When you are using laravel right join eloquent with a query builder laravel right join fetch data from the right table. Even if there is no match in the left table.
1 2 3 |
User::rightJoin('posts', 'posts.user_id', '=', 'users.id') ->select('users.*') ->get(); |
3: Laravel Cross Join Eloquent
In laravel, the cross join selects every row from the first table with every row from the second table.
1 2 |
Size::crossJoin('colours') ->get(); |
4: Laravel Advanced Join Eloquent
If in join query add some addition criteria with where clause it termed as advance join query.
1 2 3 4 5 6 |
DB::table('users') ->join('contacts', function ($join) { $join->on('users.id', '=', 'contacts.user_id') ->where('contacts.user_id', '>', 5); }) ->get(); |
5: Laravel Sub-Query Eloquent
In this laravel join example we are using join query in sub query.
1 2 3 |
DB::table('posts') ->select('user_id', DB::raw('MAX(created_at) as last_post_created_at')) ->where('is_published', true)->groupBy('user_id'); |