How to get all coupons with relation (redeemer)
stanley-hypo opened this issue · 2 comments
stanley-hypo commented
I want to get all coupons with redeemer
like:
Coupon::with(['redeemer'])->get();
thanks.
michael-rubel commented
I noticed the inconsistency in the DefinesModelRelations
trait, as this trait was supposed to define relations, not the concrete models. You're unable to eager load because it loads the model immediately in that method by using first
. The fix is PRed already and will be shipped in the next major release of the package. The delay is because it's a breaking change for those who may have overriden that method. For now, I recommend creating a separate model and extend from MichaelRubel\Couponables\Models\Coupon
, then adding a method:
public function redeemerRelation(): MorphTo
{
return $this->morphTo();
}
And you'll able to eager load it:
$coupons = Coupon::with('redeemerRelation')->get();
I hope this helps! 🙂
stanley-hypo commented
thanks so much