flower
flower

Reputation: 1059

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 !

Upvotes: 0

Views: 1765

Answers (1)

Remul
Remul

Reputation: 8242

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);

Upvotes: 1

Related Questions