简体   繁体   中英

copy record with all relations laravel 5.4

i have to copy a record from db I am trying this

$new_p = $p->replicate();
   $new_p->save();

but Its not saved P model has many relations i dont know how to copy all !

You have to attach the many to many relationships to the clone after saving it, something like this:

$new_p = $p->replicate();
$new_p->save();

$new_p->firstRelations()->attach($p->firstRelations);
$new_p->secondRelations()->attach($p->secondRelations);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM