I have the following scope that I would like to amend slightly:
function scopeNotRunOut($query)
{
return $query->has('redemptions', '<', DB::raw('quantity'));
}
This return all models where the related redemptions count is less than the quantity column. The redemptions table has a column for user_id so how would I tweak this so that it only counts redemptions where the user_id column is a given value?
Managed to work it out myself in the end, here is the answer:
function scopeCustomerMaxUsesValid($query, $user_id)
{
return $query->whereHas('redemptions', function($query) use ($user_id) {
$query->where('user_id', '=', $user_id);
}, '<', DB::raw('quantity'));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With