DEV Community

bluepine810
bluepine810

Posted on

Does not laravel eloquent have query like WhereNotHas operation?

I want to distinguish 2 queries when one field is null or not null.
Let me see one table named 'report'.
This table has some fields including 'setting' field.
When the value of 'setting' is null in row of 'report' table, The query is $query->whereHas('Setting', function($query){ $query->where('some_id', $home)}).
When the value of 'setting' is not null, The query is $query->whereHas('Device', function($query){ $query->where('some_id', $home)}).
'setting' is the reference id of table Setting.
That is to say, I want to have exculsive query in according to setting.
I made one query, but the result is not as I want.

$query = Report::query();
$query->doesntHave('Setting')->whereHas('Device', function ($q) use ($home){
$q->where('some_id', $home);
})
->orWhereHas('Setting', function ($q) use ($home) {
$q->where('some_id', $home);
});

Is there any way to have query like I want?

Top comments (0)