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...