$suppliers = Supplier::with(
[
'purcheses' => function ($query) {
$query->with(
[
'payments' => function ($query) {
$query->sum('amount');
}
]
)->get();
}
]
)->latest()->get();
I have a suppliers table with has-many relations with purchases of purchases table with has-many relations with payments and payment belongs to purchases, how to get the total sum of payment for each purchase that belongs to this supplier?
2
Answers
I think this package might help you,
The package’s readme illustrates various types of relationships that this package supports:
HasMany
ManyToMany
MorphMany
MorphToMany
MorphedByMany
BelongsTo
Here’s an example from the readme of a HasMany relationship for a complex relationship:
In the above example, the package uses Eloquent conventions keys, and the package allows you to specify custom keys for local and foreign keys.
You might want to try flatMap and loadSum: