michael-rubel/laravel-couponables

How to get all coupons with relation (redeemer)

stanley-hypo opened this issue · 2 comments

I want to get all coupons with redeemer

like:
Coupon::with(['redeemer'])->get();

thanks.

Hi @stanley-hypo

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! 🙂

thanks so much