Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent get result where relation data is null

I have two Models User and Owner with many to many relationship

I want to fetch only those users who don't have owner

how can I get using eloquent

i tried

$query = User::whereHas('userOwners', function ( $subquery ){
                $subquery->whereNull('owner_id');                            
            })->get();

but not working.

like image 834
Sharad Kale Avatar asked Aug 03 '17 12:08

Sharad Kale


1 Answers

Eloquent has a way to query an absent relationships, it should work like this in your case:

$query = User::doesntHave('userOwners')->get();
like image 184
thefallen Avatar answered Oct 15 '22 19:10

thefallen