Instead of whereHas() use has().
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
@loreias You're probably looking for the whereDoesntHave method.
User::whereDoesntHave('granteeReports', function($q){
$q->where('year', '=', 2017 );
})->get();
Please or to participate in this conversation.