Join WhatsApp ChannelJoin Now

Laravel 8 Joins Query Example

Today, i we will show you laravel 8 joins query example. This article will give you simple example of laravel 8 joins query example. you will learn laravel 8 joins query example.

Laravel joins multiple tables example. You will learn how to use laravel joins multiple tables for fetching data from database tables.

If you are new in laravel and you don’t know how to write eloquent join in laravel, then i will help you how to make eloquent join in laravel . you can easily use eloquent join in laravel 5, laravel 6, laravel 7 and laravel 8 version.

So let’s follow few step to create example of laravel 8 joins query example.

Types of Laravel 8 Joins

  • Laravel Inner Join
  • Laravel Left Join
  • Laravel Right Join
  • Laravel Cross Join
  • Laravel Advanced Join
  • Laravel Sub-Query Joins

Step 1:- Laravel Inner Join

Now learn laravel 8 Inner join, see the following examples:
Laravel Join() with 2 Tables

$users = User::join('posts', 'users.id', '=', 'posts.user_id')
               ->get(['users.*', 'posts.descrption']);

Laravel Join() with 3 Tables

$users = User::join('posts', 'posts.user_id', '=', 'users.id')
              ->join('comments', 'comments.post_id', '=', 'posts.id')
              ->get(['users.*', 'posts.descrption']);

Laravel Join() with Multiple Conditions

$users = User::join('posts', 'posts.user_id', '=', 'users.id')
            ->where('users.status', 'active')
            ->where('posts.status','active')
            ->get(['users.*', 'posts.descrption']);

Step 2:- Laravel Left Join

Now learn laravel 8 Left join, see the following examples:

$users = User::select("users.id", "users.name","users.email", "countries.name as country_name")
           ->leftJoin("countries", "countries.id", "=", "users.country_id")
           ->get();

Step 3:- Laravel Right Join

Now learn laravel 8 Right join, see the following examples:

$users = User::rightJoin('city','city.user_id','=','users.id')
          ->select('users.name','city.city_name')
         ->get();

Step 4:- Laravel Cross Join

Now learn laravel 8 Cross join, see the following examples:

$Sizes = Size::crossJoin('colours')
     ->get();

Step 5:- Laravel Advanced Join

Now learn laravel 8 Advanced join, see the following examples:

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

Step 6:- Laravel Sub-Query Joins

Now learn laravel 8 Sub-Query join, see the following examples:

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

We hope this tutorial can help you…..

Recommended Posts