If you don't want multiple records, why are you using a many to many relationship to begin with? :)
Aug 19, 2014
13
Level 11
Many to Many relation with unique records in pivot
Hi, When you use attach() method in a many to many relation, you can add this record multiple times.
Here is an example of the pivot table
id, model1, model2
1, 10, 11
2, 10, 11
And, I'd like to forbidden this behavior.
Sync() method can't be used in my case. Thanks.
Level 7
When the combination should be unique, you can remove the 'id' column from the pivot table. An 'id' column is only necessary if other fields in your table can't make up a unique value. Try removing the id column and making the primary key of your table a combination of model1 and model2
4 likes
Please or to participate in this conversation.