These will be used as relationships on your models.
class Attribute extends Model
{
/**
* Get the parent attributable model (post or video).
*/
public function attributable()
{
return $this->morphTo();
}
}
class Product extends Model
{
/**
* Get all of the product's attributes.
*/
public function attributes()
{
return $this->morphMany(Attribute::class, 'attributable');
}
}
class ProductType extends Model
{
/**
* Get all of the product type's attributes.
*/
public function attributes()
{
return $this->morphMany(Attribute::class, 'attributable');
}
}
Make sure to use the morphs() helper when defining your migration. Under the hood this defines a composite index for you which can help with speed on these tables.
In your attributes table migration:
$table->morphs('attributable');
https://laravel.com/docs/8.x/migrations#column-method-morphs