How to do IS NOT NULL in a Laravel query
13,644
Solution 1
You can use whereNotNull()
method.
https://laravel.com/docs/5.3/queries#where-clauses
Update
It seems $posts
is a collection. In this case, you'll have to use filter()
. For example:
$collection->filter(function ($item) {
return $item['some_value'] !== null;
});
Solution 2
Did you try like this...
{{$post->likes->where('post_id', $post->id)->whereNotNull('source_id')->sum('like')}}
Also try as
{{$post->likes->
where([
['post_id', '=', $post_id],
['source_id', '<>', NULL],])
->sum('like') }}
Solution 3
For Laravel 4./5. its whereNotNull() and for Laravel 3: its where_not_null().
So your query will be this if version is Laravel 4./5.,
{{$post->likes->where('post_id', $post->id)->whereNotNull('source_id')->sum('like')}}
Query will be this if version is Laravel 3,
{{$post->likes->where('post_id', $post->id)->where_not_null('source_id')->sum('like')}}
Author by
Adam
Updated on June 13, 2022Comments
-
Adam almost 2 years
I'm trying to do this SQL statement and outputting it using the blade engine:
" SELECT SUM(like) FROM likes WHERE post_id = ". $post->id. " AND source_id IS NOT '' "
I don't know how to do it, I tried this yesterday but it didn't work:
{{$post->likes->where('post_id', $post->id)->where('source_id', '<>', '')->sum('like')}}
The likes table "belongsTo" both the sources table and the posts table.
Edit:
I'm using Laravel version 5.2.45
-
linuxartisan over 7 yearsPlease specify the laravel version you are using.
-
-
Alexey Mezenin over 7 yearsThere is no
where_not_null
, it'swhereNotNull()
-
Adam over 7 yearsI get the error message
ErrorException in Macroable.php line 74: Method whereNotNull does not exist.
when I try that one. -
Adam over 7 yearsI get the error message
ErrorException in Macroable.php line 74: Method whereNotNull does not exist.
when I try that one. -
Paul Spiegel over 7 years
source_id <> NULL
will never be TRUE. -
Adam over 7 yearsI'm calling it like this from a view: ` <p>Total likes: {{ $post->likes->where('post_id', $post->id)->whereNotNull('source_id')->sum('like') }} </p> `
-
Bhavin over 7 yearsi tried with above query it's working. so please show me what you have tried you?
-
Adam over 7 years` <p>Total likes:{{$post->likes->where('post_id', $post->id)->whereNotNull('source_id')->sum('like')}} </p> ` Sorry, the code enclosing seems like it doesn't work.
-
Bhavin over 7 yearstry with removing sum(like) from above query.
-
Adam over 7 yearsI still get the error
Method whereNotNull does not exist.
-
Bhavin over 7 yearsTry with this normal query and double check the field names of your database because this error may be occurs due to field names error if it's perfect that try with this normal query in any other model and tell me below is giving the same error or not. DB::table('name')->WhereNotNull('column')->get();
-
Amin NAIRI over 7 yearsOfftopic : you should consider checking your datas in your controller instead that checking in your view.
-
Adam over 7 yearsIt seems like this is working, thanks! Still gotta do some more testing though.