1

I have made a separate Model and defined the required relationships between all my Many To Many Models:

class AttributeProduct extends Model
{
    use HasFactory;

    protected $table = 'attribute_product';

    protected $guarded = [];

    public function attribute()
    {
        return $this->belongsTo(Attribute::class, 'attribute_id', 'id');
    }

    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id', 'id');
    }

    public function value()
    {
        return $this->belongsTo(Value::class, 'value_id', 'id');
    }
}

For example, this Model is connected to attribute_products table.

And then I added this method to Product model:

public function attributeproducts()
{
    return $this->hasMany(AttributeProduct::class, 'product_id', 'id');
}

So I wonder if it is good to make a separate model and add my own methods to it or I should use Laravel pre-defined way?

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
japose7523
  • 29
  • 2
  • 15

0 Answers0