Support the ongoing development of Laravel.io →
Database Eloquent

I am having problems with creating a schema / model for one of my projects and would like to get some help here.

I have 4 tables currently : accessories , products and a pivot table product_accessory and also product_accessory_adaptor

<?php 

Schema::create('accessories', function(Blueprint $table)
{
    $table->increments('id');
}

Schema::create('products', function(Blueprint $table) 
{
    $table->increments('id');
}

Schema::create('product_accessory', function(Blueprint $table)
{
    $table->increments('id');
    $table->integer('product_id')->unsigned();
    $table->integer('accessory_id')->unsigned();
    $table->foreign('product_id')->references('id')->on('products');
    $table->foreign('accessory_id')->references('id')->on('accessories');
}

Now problem is I need to add another product type 'adaptors' that would ultimately depend on the pivot table relation, that is, adaptors need to relate to both a product and accessory...

UPDATE Here is how my current product_accessory_adaptor table is

Schema::create('product_accessory_adaptor', function(Blueprint $table)
{
    $table->increments('id');
    $table->integer('product_accessory_id')->unsigned();
    $table->foreign('product_accessory_id')->references('id')->on('product_accessory');
}

This way, i can have many adaptors relating to a product and accessory. My question is how do i model this relation in eloquent?

Here's what i have now:

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

Update Now the schema and model is setup. However there are issues with using hasManyThrough relations. In addition, any way to do eager loading in this case for the pivot relation , i.e Adaptor?

Note The error that occurs when i make a call to adaptors() on either the Product or Accessory model is Argument 1 passed to Illuminate\Database\Eloquent\Relations\Pivot::__construct() must be an instance of Illuminate\Database\Eloquent\Model, none given, called in /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 872 and defined

Last updated 3 years ago.
0

Since a product_accessory_adaptor relates to a product and a catagory, why don't you create a ProductAccessoryAdaptor model with two belongs to relationships ?

Last updated 3 years ago.
0

hi pmall, the ProductAccessoryAdaptor model would not have a direct foreign key..Could you advise how is that doable? I've updated my question as well with regards on what i've achieved so far.

Last updated 3 years ago.
0

Why is this not suited ?

Schema::create('product_accessory_adaptor', function(Blueprint $table)
{
    $table->increments('id');
    $table->integer('product_id')->unsigned();
    $table->integer('accessory_id')->unsigned();
    // more fields...
    $table->foreign('product_id')->references('id')->on('accessories');
    $table->foreign('accessory_id')->references('id')->on('accessories');
}

then

class Adaptor extends Eloquent {

    protected $table = 'product_accessory_adaptor';

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

    public function accessories() {
        return $this->belongsTo('Accessory');
    }
}
Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

parn09 parn09 Joined 21 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.