Get data from Laravel Eloquent deep association and display it in the view
P粉440453689
P粉440453689 2023-09-09 16:27:00
0
1
533

$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, How to get the total payment for each purchase from this supplier?

P粉440453689
P粉440453689

reply all(1)
P粉399090746

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template