Laravel Eloquent Left Join Where Null Condition Example
Hi
This article goes in detailed on laravel eloquent left join where null condition. i explained simply about laravel left join with where condition. let’s discuss about laravel left join where null. you can see laravel left join get null records. Let's see bellow example laravel left join where clause.
Two days ago i was working on my old project and i need to get records that not match with left join table. i mean i have users table and i need to send notification users that does not post article yet. i see laravel documentation and find out solution to how to get not match records using where null condition. so i just want to share this example with you guys so someone helps.
Let's see simple example:
SQL Query:
SELECT
*
FROM
users
LEFT JOIN
posts
ON posts.user_id = users.id
WHERE
posts.user_id IS NULL
Laravel Eloquent Query:
$data = User::leftJoin('posts', function($join) {
$join->on('posts.user_id', '=', 'users.id');
})
->whereNull('posts.user_id')
->get();
dd($data);
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Eloquent Delete Record By ID Example
- PHP Laravel Left Join Query Example
- PHP Laravel Inner Join Query Example
- Laravel Eloquent take() and skip() Query Example
- Laravel Eloquent inRandomOrder() Method Example
- Laravel Eloquent whereBetween() Query Example
- Laravel Eloquent Where Query Examples
- How to use Union Query with Laravel Eloquent?
- Laravel Many to Many Eloquent Relationship Tutorial
- Laravel Eloquent Inner Join with Multiple Conditions Example
- Laravel Join with Subquery in Query Builder Example