samedi 15 décembre 2018

Laravel - Getting Sum of Field through Polymorphic Relationship

I have two models, Payments_Distribution and Donation.

In my Donation model:

public function payments(){
    return $this->morphToMany(Payments_Distribution::class, 'payable');
}

And I can save a payment distribution to the donation model using the following:

        $distribution = new Payments_Distribution;
        $distribution->payment_id = $payment->id;
        $amount = $request->payment_details['amount'][$i];

        $donation->payments()->save($distribution);

But I'm stuck on how to retrieve the sum of all the associated records' amount fields in the Payment_Distribution model table.

Would it be something like:

$donation->payments()->______ ->sum('amount');

Or something else? I'm still a bit new to polymorphic relationships so I'd appreciate any and all help!

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire