lundi 21 septembre 2015

Laravel count relations in scope

I have a two tables discount_codes and discount_code_redemptions. The discount_codes table has an int column called max_uses that stipulates how many times a discount_code can be redeemed. Every time a discount_code is redeemed a record is created in the discount_code_redemptions table storing the discount_code_id and the user_id that redeemed it.

I want to create a scope that only returns discount_codes where max_uses hasn't been exceeded, so I need to count how many related entries there are in the discount_code_redemptions table and compare this count to the max_uses column. If it less than max_uses then the record can be returned. How would I do this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire