$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?
P粉3990907462023-09-10 14:25:08
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:
/* Country -> has many User -> has many Post -> has many Comment */ class Country extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships; public function comments() { return $this->hasManyDeep('App\Comment', ['App\User', 'App\Post']); } } // Access country comments $country->comments();
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.