Laravel Left Join Query Example

Laravel Left Join Query Example

In this example you learn laravel left join query example. In this post we will discuss about on how to implement laravel left join query builder. I simply explain you how to use left join in laravel. You will see how to use laravel left join using use db(), join() method.

Read Also :  Laravel 8 Soft Delete Example

Example : Laravel Left Join Query

public function index()
{
    
    $users =DB::table('products')
            ->leftJoin('users', 'products.user_id', '=', 'users.id')
            ->get();
    
    echo '<pre>';
    print_r($users);
    exit;
}

users Table

You have to create the users table.

users table

Products Table

product table

Output

[0] => stdClass Object
    (
        [id] => 1
        [name] => john
        [price] => 45000
        [image] => 1620826541.png
        [details] => dell laptop
        [user_id] => 1
        [created_at] => 
        [updated_at] => 
        [deleted_at] => 
        [email] => man@gmail.com
        [email_verified_at] => 
        [password] => 123456
        [remember_token] => 
    )

I hope you understand of laravel left join query and it can help you…

Read Also :  How to Get Current User Location in Laravel