Laravel 8 Left Join Query Example

Websolutionstuff | Nov-26-2021 | Categories : Laravel PHP MySQL

In this tutorial I will give you laravel 8 left join query example. laravel 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 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.

For laravel left join query example we need the first argument passed to the leftJoin method is 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.

So, let's start example of left join in laravel 8.

laravel 8 Left Join

 

SQL Query:

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

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

 

 

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 * for 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();

 

 

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
Laravel 8 Inner Join Query Example
Laravel 8 Inner Join Query Exa...

In this tutorial we will learn about laravel 8 inner join query example. Also see how to join two tables in laravel 8. I...

Read More

Nov-24-2021

How to Validate Empty Input Field in jQuery
How to Validate Empty Input Fi...

In the dynamic world of web development, form validation is a crucial aspect of creating a user-friendly and error-free...

Read More

Sep-25-2023

CRUD With Image Upload In Laravel 10 Example
CRUD With Image Upload In Lara...

In this article, we will see crud with image upload in laravel 10 examples. Here, we will learn how to image upload with...

Read More

Mar-27-2023

Send Email In Laravel
Send Email In Laravel

In this article, we will explore the process of sending emails in Laravel, covering versions 6, 7, 8, 9, and 10. Email f...

Read More

Sep-02-2020