search

Home  >  Q&A  >  body text

Get data from Laravel Eloquent deep association and display it in the view

$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粉440453689P粉440453689431 days ago445

reply all(1)I'll reply

  • P粉399090746

    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.

    reply
    0
  • Cancelreply