php Laravel Eloquent OR WHERE IS NOT NULL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26344717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:17:40  来源:igfitidea点击:

Laravel Eloquent OR WHERE IS NOT NULL

phpsqllaraveleloquentadministrator

提问by Anonymous

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:

我正在使用来自frozennode的Laravel Administrator包。长话短说,我在显示软删除的结果时遇到了问题。我正在尝试覆盖默认查询:

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:

不幸的是,我不知道如何将 orWhere() 与“不为空”一起使用。经过一番研究,我用一个原始的 SQL 块进行了尝试,如下所示:

'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():

但我最终得到了一条额外的 SQL,因为在 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?

我怎样才能解决这个问题?

回答by Jeff Lambert

Just add withTrashed:

只需添加withTrashed

'query_filter'=> function($query) {
    $query->withTrashed();
},

Source

来源

Update

更新

In that case, you can probably just add a orWhereNotNull()call:

在这种情况下,您可能只需添加一个orWhereNotNull()电话:

'query_filter'=> function($query) {
    $query->orWhereNotNull('deleted_at');
},

回答by Matthew Farrell

Eloquent has a method for (Laravel 4.* | 5.*):

Eloquent 有一个方法 (Laravel 4.* | 5.*):

Model::whereNotNull('sent_at')

Model::whereNotNull('sent_at')

Laravel 3: Model::where_not_null('sent_at')

Laravel 3: Model::where_not_null('sent_at')

回答by user4397894

If you want to search deleted record (Soft Deleted Record), don't user Eloquent Model Query.
Instead use Db::table query
e.g
Instead of using Below:

如果要搜索已删除的记录(Soft Deleted Record),请不要使用 Eloquent Model Query。
而是使用Db::table query
eg
而不是使用下面:

$stu = Student::where('rollNum', '=', $rollNum . '-' . $nursery)->first();

$stu = Student::where('rollNum', '=', $rollNum . '-' . $nursery)->first();

Use

$stu = DB::table('students')->where('rollNum', '=', $newRollNo)->first();

$stu = DB::table('students')->where('rollNum', '=', $newRollNo)->first();

回答by Gabriel Glauber

This might be relevant:

这可能是相关的:

return $this->join($table, $first, $operator, $second, 'right');

(from GitHub).

(来自GitHub)