我正在尝试克隆现有记录的集合,并为每个记录创建一个具有更改属性的新模型,例如name,promotion_id等。
$source_voice_messages = VoiceMessage::wherePromotionId($promotion_id)->get();
foreach($source_voice_messages as $source_voice_message ){
VoiceMessage::insert($source_voice_message->toArray());
}
预期的行为应该是具有新主要ID的新记录。
我得到了:
SQLSTATE[23000]: Integrity constraint violation:
1062 Duplicate entry '83' for key 'PRIMARY'
此外,我如何更改$ source_voice_message-> name
答案 0 :(得分:0)
我用replicate()
解决了这个问题$voice_message = VoiceMessage::find($source_voice_message->id);
$cloned_voice_message = $voice_message->replicate();
$cloned_voice_message->save();