Laravel Eloquent get result where relation data is null

Viewed 11159

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.

3 Answers
 User::with('userOwners')
        ->whereHas('userOwners', function ($query) {
            $query->wherehas('owner_id');
        })
        ->where('user_status', 1)->get();

use second where if you want to filter on user

use first where if you want to filter on Owner

Related