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
Laravel Eloquent OR WHERE IS NOT NULL
提问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
回答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();