I have Playlist and Track Many To Many relationship with additional order field:
Schema::create('playlist_track', function (Blueprint $table) {
$table->integer('playlist_id')->unsigned();
$table->integer('track_id')->unsigned();
$table->integer('order')->unsigned();
$table->primary(['playlist_id', 'order']);
});
User can delete tracks from playlists and change an order. So this sync method is what I need:
foreach ( $tracks as $key => $track ){
$_tracks[ $track ] = [ 'order' => $key ];
}
$playlist->tracks()->sync( $_tracks );
But I get an error when trying to change an ordering of tracks:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '3-0' for key 'PRIMARY' (SQL: update `playlist_track` set `order` = 0 where `playlist_id` = 3 and `track_id` = 1)
That's because i'm using composite primary key I guess... But I don't know how to make it works
UPD
This is dd( $_tracks )
array:2 [
1 => array:1 [
"order" => 0
]
3 => array:1 [
"order" => 1
]
]