DEV Community


Posted on • Originally published at

Left Join Query In Laravel 9 Example

In this article, we will see the left join query in the laravel 9 examples.

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 the left join with groupBy() in laravel 9. 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 laravel 9 left join query, join query in laravel 9, left join in laravel 9, laravel 9 join two tables, laravel 9 left join group by, laravel left join subquery, left join query in MySQL.

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, and laravel 9.

SQL Query:

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

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Enter fullscreen mode Exit fullscreen mode

Read Also: Inner Join Query In Laravel 9 Example

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`
Enter fullscreen mode Exit fullscreen mode

Laravel Query:

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

public function index()
    $users = User::select('', '', '', ' as country_name')
                    ->leftJoin('countries', '', '=', 'users.country_id')
Enter fullscreen mode Exit fullscreen mode

Using DB:

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

$users = DB::table('users')
            ->rightJoin('posts', '', '=', 'posts.user_id')
Enter fullscreen mode Exit fullscreen mode

Read Also: Group By Query In Laravel 9 Example

Laravel Left Join with Multiple Condition

Now, we will give an example of a multiple where conditions with an aggregate function.

User::leftJoin('posts', '', '=', 'posts.user_id')
       ->where('is_published', true)
Enter fullscreen mode Exit fullscreen mode

You might also like:

Read Also: How To Get Current Month Records In MySQL Query
Read Also: Laravel 8 One To Many Relationship Example
Read Also: Laravel REST API CRUD Tutorial

If this post was helpful please like, share and comment✌️

Top comments (0)