Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

loreias's avatar

where value is not equal to.

Hi every body, got a question. I'm trying create a query where i fetch only the users with that has an specific relationship and them filter down only the ones that does not have a year value equal to the pass variable. Not quite sure why every time it returns all the users with the relationship but does not filter down the year. here is the code.

        User::whereHas('granteeReports',  function($q){
             $q->where('year', '=',  2017 );
        })->get();

returns 2 records ( expected );

        User::whereHas('granteeReports',  function($q){
             $q->where('year', '<>',  2017 );
        })->get();

returns all records of user with granteeReports relationship ( expecting two records diffrent than what previus query returns)

Here are the relationships:

public function granteeReports ()
    {
        return $this->hasMany('App\GranteeReport');
    }

    public function user ()
    {
        return $this->belongsTo(App\User::class);       
    }

Appreciate any help with it, thanks guys

0 likes
10 replies
loreias's avatar

@jekinney Thanks for tip, but not quite sure if i get you, if i change the whereHas() for has() them i filter down with where

 App\User::has('granteeReports')->where('year', '<>', 2017)->get()

i get the following error

Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'year' in 'where clause' (SQL: select * from `users` where (select count(*) from `grantees_reports` where `grantees_reports`.`user_id` = `users`.`id`) >= 1 and `year` <> 2017)'

not sure if that is how it supoused to be done.

willvincent's avatar

Try

App\User::has('granteeReports')->where('grantees_reports.year', '<>', 2017)->get()
cipsas's avatar

First atempt should work, try to

User::whereHas('granteeReports',  function($q){
             $q->where('year', '!=',  2017 );
        })->get();
loreias's avatar

Thanks guys. @willvincent , got this error :

Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'grantees_reports.year' in 'where clause' (SQL: select * from `users` where (select count(*) from `grantees_reports` where `grantees_reports`.`user_id` = `users`.`id`) >= 1 and `grantees_reports`.`year` <> 2017)'

@cipsas As you said, it should work, but for some reason the query returns all the users with reportsGrantee relationship, and does not filter down the year, here is the generate it query

select * from `users` where (select count(*) from `grantees_reports` where `grantees_reports`.`user_id` = `users`.`id` and `year` != ?) >= 1
willvincent's avatar

Perhaps a silly question, but are you sure the year column is an integer?

vandyczech's avatar

You can try this breakneck solution with chaining where condition, something like this:

->where('year', '<',  2017 )->where('year', '>',  2017 )->get();

Year is less than and greater than, and the condition is completed, So, the specified year is not equal

thomaskim's avatar
Level 41

@loreias You're probably looking for the whereDoesntHave method.

User::whereDoesntHave('granteeReports',  function($q){
     $q->where('year', '=',  2017 );
})->get();
4 likes

Please or to participate in this conversation.