314

How do you check if a field is not null with Eloquent?

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

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

  '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
Marwelln
  • 28,492
  • 21
  • 93
  • 117
  • You could try using `!=` instead of IS NOT. – JaTochNietDan Jan 22 '14 at 11:28
  • 2
    @JaTochNietDan The != operator does not work with NULL values. Per the [MySQL Documentation](http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html): "You cannot use arithmetic comparison operators such as =, <, or <> to test for NULL." – Soulriser Jan 12 '16 at 22:10

11 Answers11

587

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

Model::whereNotNull('sent_at')

Laravel 3:

Model::where_not_null('sent_at')
ishegg
  • 9,685
  • 3
  • 16
  • 31
Bas
  • 5,904
  • 1
  • 15
  • 8
  • 1
    I need to check the null at deleted field, so I changed this into `whereNull('deleted_at')` and I got my query running. – Tarunn Jun 19 '15 at 12:45
  • 8
    Another [undocumented](http://laravel.com/docs/4.2/queries) feature. Well, unless you count the API docs, but Laravel's main documentation makes no mention of it. – aross Jul 01 '15 at 12:38
  • 14
    The `whereNotNull()` method (and several others that were undocumented previously) were added to the documentation in version 5.1: http://laravel.com/docs/5.1/queries#where-clauses . – Ben Johnson Jul 09 '15 at 20:52
  • @aross but in Query Builder, not in Eloquent (Laravel 5.1) – pmiranda Oct 16 '19 at 17:26
  • @pmiranda not sure what you mean, but my comment was 4 years ago and about Laravel 4, nowadays we're at Laravel 6. I guess the situation changed. Although Laravel's main documentation is still not very complete, it feels more like a bunch of guides. – aross Oct 17 '19 at 08:23
  • "whereNotNull()" works in Laravel 7 and 8 also. I have also tested it with Lumen 8. Thanks. – Kamlesh Jun 01 '21 at 17:12
  • If you're testing `deleted_at IS NOT NULL` this is a default filter for all queries if your models `use SoftDeletes;` – qu1j0t3 Oct 10 '22 at 20:56
24

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

 $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', '<>', '');
}
Atiqur
  • 3,802
  • 1
  • 25
  • 26
  • 2
    what does `'and'` stands for? – senty Sep 29 '16 at 22:24
  • 1
    Actually here 'and' does nothing but it would do if first parameter would be an array. Here is the method prototype : `public function where($column, $operator = null, $value = null, $boolean = 'and'); `and location - `".....\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php"` – Atiqur Oct 02 '16 at 05:05
19

We can use

Model::whereNotNull('sent_at');

Or

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

If you wanted to use the DB facade:

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

Jon
  • 2,277
  • 2
  • 23
  • 33
11

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.2 and Laravel 5.7. or possible because I was just playing around with some data on the CLI using Laravel Tinker.

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


I did not have success running:
    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


I did however have success running:
    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\Collection instead of an array if you prefer.

Rockin4Life33
  • 2,240
  • 1
  • 31
  • 29
7

in laravel 5.4 this 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.

JON
  • 965
  • 2
  • 10
  • 28
4

Checking "not null"

Model::whereNotNull('column1');

Checking "null"

Model::whereNull('column1');

Another solution is using whereRaw()

Model::whereRaw("column1 IS NOT NULL");
Model::whereRaw("column1 IS NULL");
bluestar0505
  • 338
  • 1
  • 14
3

You can do it by simply following:

Model::whereNotNull('sent_at')->get();
tayeb320
  • 115
  • 1
  • 10
1

You can also use raw query.

Remember: Reports is my model and I am using where raw, the best thing of the raw query is that you can use multiple types of operators such as AND, OR etc just by passing as a string.

For example: WHERE condition1 AND condition2 AND condition3 ...;


Reports::whereRaw("column1 IS NOT NULL AND column2 IS NOT NULL");

The above query will be executed as:

Select * from reports where column IS NOT NULL AND column2 IS NOT NULL.

For more understanding of IS Null and IS not null operators:

https://www.w3schools.com/sql/sql_and_or.asp

Sabaoon Bedar
  • 3,113
  • 2
  • 31
  • 37
0
$deliveryTo = DeliveryTo::where([
            ['delete_flg', '=', 0],
            ['area_id', '=', 1],
            ['area_id', '!=', NULL],
        ])->get();
Ram Pukar
  • 1,583
  • 15
  • 17
-17

If you want to search deleted record (Soft Deleted Record), do not use Eloquent Model Query.

Use the Db::table query instead. E.g.:

Instead of:

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

Use:

$stu = DB::table('students')->where('rollNum', '=', $newRollNo)->first();
user16780927
  • 93
  • 1
  • 1
  • 15
  • This answer has nothing to do with the question. Besides that, you don't necessary needs to use `Db::table` to find the soft deleted records. You could filter them with the method `withTrashed()`, like is told in the docs: https://laravel.com/docs/5.3/eloquent – gvsrepins Nov 07 '16 at 19:01
  • This answer has nothing to do with the demanded question. – Sabaoon Bedar Oct 18 '21 at 08:07