3

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?

1 Answer 1

3

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'));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.