replicate()
っていうのを使う。
こんな感じ。
$user = User::find(1)
$new_user = $user->replicate();
$new_user->name = 'new name';
$new_user->save();
内部的には、こんな感じで実装されている。
/**
* Clone the model into a new, non-existing instance.
*
* @param array $except
* @return \Illuminate\Database\Eloquent\Model
*/
public function replicate(array $except = null)
{
$except = $except ?: [
$this->getKeyName(),
$this->getCreatedAtColumn(),
$this->getUpdatedAtColumn(),
];
$attributes = array_except($this->attributes, $except);
with($instance = new static)->setRawAttributes($attributes);
return $instance->setRelations($this->relations);
}
参考)
http://stackoverflow.com/questions/18322214/laravel4-duplicate-copy-table-row