Laravel 5.4钥匙和#x27的重复输入;初级';关于sync()
我有Laravel 5.4钥匙和#x27的重复输入;初级';关于sync(),laravel,eloquent,laravel-5.4,Laravel,Eloquent,Laravel 5.4,我有播放列表和曲目与附加顺序字段的多对多关系: Schema::create('playlist_track', function (Blueprint $table) { $table->integer('playlist_id')->unsigned(); $table->integer('track_id')->unsigned(); $table->integer('order')->unsigned(); $tabl
播放列表
和曲目
与附加顺序
字段的多对多关系:
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']);
});
用户可以从播放列表中删除曲目并更改顺序。所以我需要的就是这个sync
方法:
foreach ( $tracks as $key => $track ){
$_tracks[ $track ] = [ 'order' => $key ];
}
$playlist->tracks()->sync( $_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)
那是因为我用的是复合主键我猜。。。但我不知道如何让它工作
UPD
这是dd($\u轨道)
使用自动递增整数列作为主键:
$table->increments('id');
因为它是一个透视表,所以不能将外键定义为主键,所以请删除以下内容:
$table->primary(['playlist_id', 'order'])
Pivot表可以有非唯一的播放列表id
值,但使用->primary()
您正在创建一个约束。$table->dropPrimary()
如果您在以后编写迁移以删除它。
$table->primary(['playlist_id', 'order'])