Impatient loading a nested relationship using a hinge

I currently have 4 tables:

accessories 
id 

products 
id

product_accessory 
id 
product_id
accessory_id 

product_accessory_adaptor
id 
product_accessory_id 

Matching them to Eloquent models gives me

Custom summary model:

class ProductAccessory extends Pivot {
   protected $table = 'product_accessory';

   public function product()
   {
      return $this->belongsTo('Product');
   }

   public function accessory()
   {
     return $this->belongsTo('Accessory');
   }

   public function adaptors() {
     return $this->hasMany('Adaptor', 'product_accessory_id'); 
   } 
}

Product and Accessories Model

class Accessory extends Eloquent {

   public function products()
   {
      return $this->belongsToMany('Product', 'product_accessory', 'accessory_id', 'product_id')->withPivot();
   }

   public function newPivot(Eloquent $parent, array $attributes, $table, $exists) 
   {
      if ($parent instanceof Product) {
          return new ProductAccessory($parent, $attributes, $table, $exists);
      }
      return parent::newPivot($parent, $attributes, $table, $exists);
   }

   public function adaptors()
   {
      return $this->hasManyThrough('Adaptor', 'ProductAccessory', 'accessory_id', 'product_accessory_id');
   }
}

class Product extends Eloquent {

   public function accessories()
   {
      return $this->belongsToMany('Accessory', 'product_accessory', 'product_id', 'accessory_id')->withPivot();
   }

   public function newPivot(Eloquent $parent, array $attributes, $table, $exists) 
   {
      if ($parent instanceof Accessory) {
          return new ProductAccessory($parent, $attributes, $table, $exists);
      }
      return parent::newPivot($parent, $attributes, $table, $exists);
   }

   public function adaptors()
   {
      return $this->hasManyThrough('Adaptor', 'ProductAccessory', 'product_id', 'product_accessory_id');
   }
}

Adapter model:

class Adaptor extends Eloquent {

   protected $table = 'product_accessory_adaptor';

   public function productAccessory() {
      return $this->belongsTo('ProductAccessory');
   }
}

This is what I need. Strive to download all products with accessories with adapters belonging to their axis. I tried something like this, but I don't know how to pass in the summary id

in product model

public function accessories()
{
    return $this->belongsToMany('Compatibility\Accessory', 'product_accessory', 'product_id', 'accessory_id')
                ->withPivot('id', 'accessory_disclaimer')
                ->withTimestamps()
                ->with(['adaptors' => function ($q) {
                    $q->where('product_accessory_id', $this->pivot->id);                       
                }]);
}

Does anyone know how to do this? What should I replace with $ this-> pivot-> id?

0
source share
1 answer

There is no need to impatient or load associated with the rotation model. This will not work like this, so I suggest the following:

$products->load('ProductAccessory.accessory', 'ProductAccessory.adaptors');

// then you access related accessory and adaptors through the pivot model
$product = $products->first();

$product->productAccessory->accessory;
$product->productAccessory->adaptors;

// Product model
public function productAccessory()
{
  return $this->hasMany('ProductAccessory');
}

: ProductAccessory Eloquent (Model), Pivot - .

, .. .

- :

$product->accessory; // accessory model, so far so good
$product->adaptors; // all adaptors, not so good anymore

// to get adaptors for the accessory we need:
$accessories = $product->adaptors()->where('product_accessories.id', $product->accessory->pivot->id)->get();

..

+1

All Articles