laravel Laravel4 重复/复制表格行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18322214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:18:21  来源:igfitidea点击:

Laravel4 duplicate / copy table row

phplaravellaravel-4

提问by kilrizzy

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:

我确信必须有一种更快的方法来执行以下操作。我无法找到有关如何在不覆盖现有项目的情况下将 Laravel 模态对象另存为新行的任何信息。本质上,我现有的代码更简单:

$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:

相反,复制除行的 id 之外的所有内容:

$oldItem = Item::find(1);
$newItem = $oldItem;
unset($newItem->id);
$newItem->save();

回答by peterm

You can try

你可以试试

$newItem = Item::find(1)->replicate()->save();