是否有任何方法可以轻松地克隆一个 Eloquent 对象,包括它的所有关系?
例如,如果我有这些表:
users ( id, name, email )
roles ( id, name )
user_roles ( user_id, role_id )
In addition to creating a new row in the users
table, with all columns being the same except id
, it should also create a new row in the user_roles
table, assigning the same role to the new user.
就像这样:
$user = User::find(1);
$new_user = $user->clone();
哪里有用户模型
class User extends Eloquent {
public function roles() {
return $this->hasMany('Role', 'user_roles');
}
}