Laravel Eloquent - Where Relationship Field Does Not Equal
I thought this would be fairly simple but it's not playing ball currently. I have 2 tables for this question, 'applications' & 'application_call_logs'. This query needs to retu
Solution 1:
there is a solution around should be good for this situation:
i think that this line has the week point of the code:
return $this->hasOne('App\ApplicationCallLog', 'lead_id', 'id')->latest();
this should be hasMany, but you use hasOne to limit the result to one.
and if you tried:
return$this->hasMany('App\ApplicationCallLog', 'lead_id', 'id')->latest()->limit(1);
it simply won't work, because the result will be limited to ApplicationCallLog for all of the results ....
will, there is a package staudenmeir/eloquent-eager-limit that is made especially for this situations:
composer require staudenmeir/eloquent-eager-limit:"^1.0"classApplicationextendsModel{
use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
publicfunctionlatest_call_log()
{
return$this->hasMany('App\ApplicationCallLog', 'lead_id', 'id')->latest()
->limit(1);
}
}
classApplicationCallLogextendsModel{
use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
}
using this package will limit ApplicationCallLog for every result in your query not one for all of the result, and that will have the same effect for hasOne ....
with this minor enhancement, i think:
$q->where('status', '!=', 'not interested');
will work ...
more about eloquent-eager-limit package in:
Post a Comment for "Laravel Eloquent - Where Relationship Field Does Not Equal"