laravel 你如何使用 Eloquent 检查“if not null”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21281504/
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-09-09 02:43:15  来源:igfitidea点击:

How do you check "if not null" with Eloquent?

laraveleloquent

提问by Marwelln

How do you check if a field is not nullwith Eloquent?

如何使用 Eloquent检查字段是否为空

I tried Model::where('sent_at', 'IS NOT', DB::raw('null'))->...but it gives IS NOTas a binding instead of a comparison.

我尝试过,Model::where('sent_at', 'IS NOT', DB::raw('null'))->...但它IS NOT作为绑定而不是比较提供。

This is what DB::getQueryLog()says about it:

这是DB::getQueryLog()关于它的内容:

  'query' => string 'select * from my_table where sent_at = ? and profile_id in (?, ?) order by created_at desc' (length=101)
  'bindings' => 
    array (size=3)
      0 => string 'IS NOT' (length=6)
      1 => int 1
      2 => int 4

回答by Bas

Eloquent has a method for that (Laravel 4.*/5.*);

Eloquent 有一个方法 (Laravel 4.*/5.*);

Model::whereNotNull('sent_at')

Laravel 3:

Laravel 3:

Model::where_not_null('sent_at')

回答by Atiqur

If someone like me want to do it with query builder in Laravel 5.2.23 it can be done like ->

如果像我这样的人想在 Laravel 5.2.23 中使用查询构建器来完成它可以像 ->

 $searchResultQuery = Users::query(); 
 $searchResultQuery->where('status_message', '<>', '', 'and'); // is not null
 $searchResultQuery->where('is_deleted', 'IS NULL', null, 'and'); // is null 

Or with scope in model :

或者在模型中使用范围:

public function scopeNotNullOnly($query){

    return $query->where('status_message', '<>', '');
}

回答by Jon

If you wanted to use the DB facade:

如果你想使用数据库门面

DB::table('table_name')->whereNotNull('sent_at')->get();

DB::table('table_name')->whereNotNull('sent_at')->get();

回答by srmilon

We can use

我们可以用

Model::whereNotNull('sent_at');

Or

或者

Model::whereRaw('sent_at is not null');

回答by Rockin4Life33

I see this question is a bit old but I ran across it looking for an answer. Although I did not have success with the answers here I think this might be because I'm on PHP 7.2and Laravel 5.7.or possible because I was just playing around with some data on the CLI using Laravel Tinker.

我看到这个问题有点老了,但我遇到了它寻找答案。虽然我在这里的答案没有成功,但我认为这可能是因为我使用的是PHP 7.2Laravel 5.7。或者可能是因为我只是在 CLI 上使用 Laravel Tinker 处理一些数据。

I have some things I tried that worked for me and others that did not that I hope will help others out.

我尝试了一些对我有用的方法,而另一些则没有,我希望能帮助其他人。



我没有成功运行:

    MyModel::whereNotNull('deleted_by')->get()->all();             // []
    MyModel::where('deleted_by', '<>', null)->get()->all();        // []
    MyModel::where('deleted_by', '!=', null)->get()->all();        // []
    MyModel::where('deleted_by', '<>', '', 'and')->get()->all();   // []
    MyModel::where('deleted_by', '<>', null, 'and')->get()->all(); // []
    MyModel::where('deleted_by', 'IS NOT', null)->get()->all();    // []

All of the above returned an empty array for me

以上所有都为我返回了一个空数组



但是,我确实成功运行了:

    DB::table('my_models')->whereNotNull('deleted_by')->get()->all(); // [ ... ]

This returned all the results in an array as I expected. Note: you can drop the all()and get back a Illuminate\Database\Eloquent\Collectioninstead of an array if you prefer.

这按照我的预期返回了数组中的所有结果。注意:如果您愿意,您可以删除all()并取回Illuminate\Database\Eloquent\Collection而不是数组。

回答by Carding Ermita Sungkit

in laravel 5.4this code Model::whereNotNull('column')was not working you need to add get()like this one Model::whereNotNull('column')->get();this one works fine for me.

laravel 5.4 中,此代码Model::whereNotNull('column')不起作用,您需要get()像这样添加此代码,Model::whereNotNull('column')->get();这对我来说很好用。

回答by user4397894

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

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

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

Use:

用:

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