Laravel Eloquent OR WHERE IS NOT NULL
Solution 1
Just add withTrashed
:
'query_filter'=> function($query) {
$query->withTrashed();
},
Update
In that case, you can probably just add a orWhereNotNull()
call:
'query_filter'=> function($query) {
$query->orWhereNotNull('deleted_at');
},
Solution 2
Eloquent has a method for (Laravel 4.* | 5.*):
Model::whereNotNull('sent_at')
Laravel 3:
Model::where_not_null('sent_at')
Anonymous
Updated on October 22, 2020Comments
-
Anonymous over 3 years
I am using the Laravel Administrator package from frozennode. Long story short, I'm running into issues when displaying results that are soft-deleted. I'm trying to override the default query:
select * from `scripts` where `scripts`.`deleted_at` is null group by `scripts`.`id`
To display both deleted results and non-deleted, somehow hacking away. It's not the most elegant solution but I don't see any other way to do it. So, my goal is to do this:
select * from `scripts` where `scripts`.`deleted_at` is null or `scripts`.`deleted_at` is not null group by `scripts`.`id`
Unfortunately I don't know how to use orWhere() with 'is not null'. After researching a bit, I tried it with a raw SQL block, like this:
'query_filter'=> function($query) { $query->orWhere(DB::raw('`scripts`.`deleted_at` is not null')); },
But I ended up with an extra piece of SQL resulting of not including the second parameter in orWhere():
select * from `scripts` where `scripts`.`deleted_at` is null or `scripts`.`deleted_at` is not null **is null** group by `scripts`.`id`
How can I fix this?
-
Anonymous over 9 yearsUnfortunately that doesn't work. I'm getting a Call to undefined method Illuminate\Database\Query\Builder::withTrashed() error.