laravel manyToMany multikey

34 Views Asked by At

I have four tables

quotation

  • id

item

  • id
  • quotation_id

article

  • article_id
  • quotation_id

articleItem

  • pivot_item_id
  • pivot_article_id

either articleItem have more columns

and i need right syntax of manyToMany relation between item and article

at the moment i have in item model

return $this->belongsToMany(Article::class,'articleItem','pivot_item_id','pivot_article_id','id','article_id');

i really dont know where to put quotation_id in this relation... is it possible?

2

There are 2 best solutions below

5
Shaun McCoy On

How is item related to article? Is an article assigned to an item or vice versa? More information would be helpful. Maybe give an example of what you are trying to achieve.

Many-to-many relationship is usually 3 tables such as users, roles, role_users. What you have looks similar to a polymorphic many-to-many with item and article sharing a relationship with quotation.

0
Petr Kamen Spinar On

solved by adding

->whereHas('quotation', function ($query) {$query->where('id', $this->quotation_id);})

at the end of declaration of relation.