Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel4 duplicate / copy table row

I'm sure there has to be a quicker way to do the following. I wasn't able to find anything about how to save a laravel modal object as a new row without overwriting the existing item. Essentially, a simpler of my existing code:

$oldItem = Item::find(1);
$newItem = new Item;
$newItem->key = $oldItem ->key;
$newItem->name = $oldItem ->name;
$newItem->path = $oldItem ->path;
$newItem->save();

Instead, copying everything but the id of the row:

$oldItem = Item::find(1);
$newItem = $oldItem;
unset($newItem->id);
$newItem->save();
like image 551
kilrizzy Avatar asked Aug 19 '13 20:08

kilrizzy


1 Answers

You can try

$newItem = Item::find(1)->replicate()->save();
like image 50
peterm Avatar answered Nov 16 '22 02:11

peterm