$suppliers = Supplier::with( [ 'purcheses' => function ($query) { $query->with( [ 'payments' => function ($query) { $query->sum('amount'); } ] )->get(); } ] )->latest()->get();
I have a supplier table which contains many relationships with purchasing table which contains many relationships with payment, and payment belongs to purchasing, strong> How to get the total payment for each purchase from this supplier?
I think this package may be helpful to you,
The package's readme file describes the various relationship types supported by the package:
There are many
Many to Many
MorphMany
MorphToMany
MorphedByMany
belong
The following is an example from the readme file for a HasMany relationship for a complex relationship:
In the example above, the package uses the Eloquent convention keys, and the package allows you to specify custom keys for local and foreign keys.