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?