I have a 3-way pivot table with character_id, item_id and location_id with a primary key over all three columns. If I mistakenly enter entries twice I get a "Integrity constraint violation: 19 UNIQUE constraint failed" error.
This is how I store entries: $location->characterItems()->attach($item->id, ['character_id' => $character->id]);
Is there something similar like updateOrCreate for pivot tables? Ideally, the duplicate entry would be overwritten or skipped. Any way to achieve this?
At the end I've added false, this will enable syncWithoutDetaching because by default syncWithPivotValues will detach values that are not included in the method.
By enabling syncWithoutDetaching the value for location id won't be removed from the specific pivot's relation database record.
If for any reason you don't want this behavior and location_id should be removed if not included you can remove false
@gych Thank you for your example, it works now. (Though I had to reverse the order and start with the character to get the expected result, but that's on me)