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?