We can avoid Laravel query builder joining to fetch relational data from the database in Laravel. But sometimes we need Laravel join with the query builder. In this case, if you are an eloquent lover and do not know how to write a join query in Laravel, then this Laravel join, Laravel left join, and Laravel inner join tutorial will help you.

In this tutorial, I will show you some join query examples with query builder in Laravel so that you can write join queries very easily. I will show you the laravel join example then the laravel left join example then the laravel inner join example then laravel join two tables with SQL join query with an example.

By default join() creates an inner join query in Laravel. If you want to create left or right join then you have to use leftJoin() and rightJoin() functions to join multiple tables in Laravel.

laravel-inner-join-left-join-right-join-example-with-query

Let's see the first example of a laravel join or laravel inner join query. To perform a basic "inner join", we can use the join method on a query builder instance

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function __invoke()
    {
        $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();

        return $users;
    }
}

 

Let's see the first example of a laravel left join query. To perform a basic "left join", we can use the leftJoin() method on a query builder instance

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function __invoke()
    {
        $users = DB::table('users')
            ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
            ->get();

        return $users;
    }
}

 

Let's see the first example of a laravel right join query. To perform a basic "right join", we can use the rightJoin() method on a query builder instance

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function __invoke()
    {
        $users = DB::table('users')
            ->rightJoin('posts', 'users.id', '=', 'posts.user_id')
            ->get();

        return $users;
    }
}

 

Read also: Laravel Subquery Join With Laravel DB Raw Example

 

Let's see the first example of a laravel cross join query. To perform a basic "cross join", we can use the crossJoin() method on a query builder instance

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function __invoke()
    {
        $sizes = DB::table('sizes')
            ->crossJoin('colors')
            ->get();

        return $users;
    }
}

 

Read also: Laravel Where OrWhere WhereNull WhereIn Where Not Query Example

 

Conclusion

I have tried to demonstrate the concept of laravel join query. We have seen laravel inner join, left join, right join and cross join examples with query. Hope this laravel join query tutorial will help you.

Category : #laravel

Tags : #laravel , #laravel query