1
votes

I have 4 tables currently :

accessories 
id 

products 
id

product_accessory 
id 
product_id
accessory_id 

product_accessory_adaptor
id 
product_accessory_id 

Mapping them to Eloquent models gives me

Custom pivot 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 Accessory 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');
   }
}

Adaptor model:

class Adaptor extends Eloquent {

   protected $table = 'product_accessory_adaptor';

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

Here's what i need. Eager loading all products with accessories with adaptors belonging to their pivot. I've tried something like this but i have no clue how to pass in the pivot 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 has any clue how to do this? What should i replace with $this->pivot->id?

1
Please refer to the comments in your previous question about the Pivot model. Hope it's all clear.Jarek Tkaczyk

1 Answers

1
votes

There is no need to eager load anything related to the pivot model. It's not gonna work this way, so I suggest this:

$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');
}

note: ProductAccessory should extend Eloquent (Model), not Pivot - like I stated my answer to your previous question.

This is the way to have this in one place, ie. get related accessory and associated adaptors.

Otherwise you would need something like this:

$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();

not very convenient..