staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels
MIT License
2.67k stars 157 forks source link

Will this work for me? #125

Closed airliajsmith closed 3 years ago

airliajsmith commented 3 years ago

Purchase Orders can have multiple shipments. Shipments can send multiple Purchase Orders at one time.

Both are related to Vendors. Right now, I have the vendor setup through the Purchase Orders.

How do I use your package in this situation?

staudenmeir commented 3 years ago

Have you already defined these relationships?

airliajsmith commented 3 years ago

Yes, but I'm not certain I did it correctly.

In Purchase Model:

public function shipments()
    {
        return $this->belongsToMany('App\Models\Purchasing\Shipment', 'purchase_shipment');
    }

    public function vendor()
    {
        return $this->belongsTo('App\Models\Vendor\Vendor');
    }

In Vendor Model:

public function shipments()
    {
        return $this->hasManyThrough('App\Models\Purchasing\Shipment', 'App\Models\Purchasing\Purchase',
            'shipments.id', 'purchases.vendor_id');
    }

Shipment Model:

public function purchase(){
        return $this->belongsToMany('App\Models\Purchasing\Purchase', 'purchase_shipment');
    }

    public function vendor(){
        return $this->hasOneThrough('App\Models\Vendor\Vendor', 'App\Models\Purchasing\Purchase',
            'purchases.vendor_id', 'purchases.id');
    }
staudenmeir commented 3 years ago

What relationship are looking to define?