You can create a custom pivot model and assign a get accessor for this. It avoids using any more queries than it needs to.
class QualificationsPivot extends Pivot{
public function getValidAttribute(){
return $this->attributes['valid_until'] <= $this->atttributes['days_before_warning']
}
}
As long as you modify your relationship method on you user model to include using():
return $this->belongsToMany(Qualifications::class, 'qualifications_pivot_table_name', 'user_id', 'qualifications_id')
->using(QualificationsPivot::class)
->withPivot(['valid_until', 'days_before_warning']);
This should allow you to call:
$this->Qualifications->where('pivot.valid', true);
To get all of the qualifications where the mutated valid attribute is true. I'm not sure what your relationship method looks like, but I'm assuming you are eager loading qualifications using a with() method. I tested it on one of my pivot models and it seems to be working fine. Let me know if you have any further questions. See some references below:
using() method:
https://medium.com/@codebyjeff/custom-pivot-table-models-or-choosing-the-right-technique-in-laravel-fe435ce4e27e
Some SO question I found that could be useful for this (mutators on pivot models):
laravel/eloquent mutators/accessors in a pivot table
Edit: Also came across this: https://laracasts.com/discuss/channels/eloquent/how-to-define-an-accessor-for-a-pivot-table-attribute?page=1
it states that you should just be able to provide an accessor on the parent model (user). I have not tested it though, but it may be a simpler option if you do not need the other features that come with a custom pivot model.