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')}}
Share:
13,644
Adam
Author by

Adam

Updated on June 13, 2022

Comments

  • Adam
    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
      linuxartisan over 7 years
      Please specify the laravel version you are using.
  • Alexey Mezenin
    Alexey Mezenin over 7 years
    There is no where_not_null, it's whereNotNull()
  • Adam
    Adam over 7 years
    I get the error message ErrorException in Macroable.php line 74: Method whereNotNull does not exist. when I try that one.
  • Adam
    Adam over 7 years
    I get the error message ErrorException in Macroable.php line 74: Method whereNotNull does not exist. when I try that one.
  • Paul Spiegel
    Paul Spiegel over 7 years
    source_id <> NULL will never be TRUE.
  • Adam
    Adam over 7 years
    I'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
    Bhavin over 7 years
    i tried with above query it's working. so please show me what you have tried you?
  • Adam
    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
    Bhavin over 7 years
    try with removing sum(like) from above query.
  • Adam
    Adam over 7 years
    I still get the error Method whereNotNull does not exist.
  • Bhavin
    Bhavin over 7 years
    Try 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
    Amin NAIRI over 7 years
    Offtopic : you should consider checking your datas in your controller instead that checking in your view.
  • Adam
    Adam over 7 years
    It seems like this is working, thanks! Still gotta do some more testing though.