Laravel 9 Left Join Query Example

Websolutionstuff | Mar-31-2022 | Categories : Laravel PHP MySQL

In this article, we will see the laravel 9 left join query example. laravel 9 left join eloquent returns all rows from the left table, even if there are no matches in the right table, The result is NULL from the right side. We will also see query of laravel 9 left join with groupBy(). If you would like to perform a "left join" or "right join" instead of an "inner join", use the leftJoin or rightJoin methods. These methods have the same signature as the join method.

So, let's see examples of left join in laravel 9, laravel 9 join two tables, left join in laravel 9, laravel 9 left join query, laravel 9 left join group by, left join query in MySQL, left join query in PHP, laravel left join subquery, laravel 9 left join query, left join in laravel 9.

For the laravel 9 left join query example we need the first argument passed to the leftJoin method to be the name of the table you need to join to, while the remaining arguments specify the column constraints for the join. Left join query use in laravel 6, laravel 7, laravel 8, laravel 9.

laravel_9_left_join

 

SQL Query :

In this example, we will create users table and countries table. we will add the country_id  foriegn key on the user's table. So, when I get users at that time we will get country name from country_id using left join.

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

 

 

The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from the right table (table2). The result is 0 records from the right side, if there is no match.

select `users`.`id`, `users`.`name`, `users`.`email`, `countries`.`name` as `country_name` 
from `users` 
left join `countries` on `countries`.`id` = `users`.`country_id`

 

Laravel Query :

In this example, select data like id, name email, and country name but if you want all fileds then you can use * to select all data.

public function index()
{
    $users = User::select('users.id', 'users.name', 'users.email', 'countries.name as country_name')
                    ->leftJoin('countries', 'countries.id', '=', 'users.country_id')
                    ->get();
}

 Using DB:

$users = DB::table('users')
            ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
            ->get();

$users = DB::table('users')
            ->rightJoin('posts', 'users.id', '=', 'posts.user_id')
            ->get();

 

 

Laravel Left Join with Multiple Condition

Now, I will give example about multiple where condition with aggregate function.

User::leftJoin('posts', 'users.id', '=', 'posts.user_id')
       ->select('users.*')
       ->where('is_published', true)
       ->where('views','>=','100')
       ->get();

 


You might also like :

Recommended Post
Featured Post
Unveiling Affordable Managed Hosting Plans For You
Unveiling Affordable Managed H...

In today's digital landscape, a strong online presence is essential for everyone. It's no longer an option; it&#...

Read More

Jun-29-2024

Laravel 8 Many To Many Relationship Example
Laravel 8 Many To Many Relatio...

In this example we will see laravel 8 many to many relationship example. Use many to many relationship in lara...

Read More

Nov-15-2021

How To Add Toastr Notification In Laravel 10
How To Add Toastr Notification...

In this article, we will see how to add toastr notification in laravel 10. Here, we will learn about toastr notification...

Read More

Mar-06-2023

Laravel 8 Socialite Login with Google Account
Laravel 8 Socialite Login with...

In this article, we will see laravel 8 socialite login with a google account. This post gives you an example of a larave...

Read More

Dec-01-2020