Clone an Eloquent object including all relationships?

后端 未结 10 688
我寻月下人不归
我寻月下人不归 2020-12-01 01:24

Is there any way to easily clone an Eloquent object, including all of its relationships?

For example, if I had these tables:

users ( id, name, email          


        
10条回答
  •  不思量自难忘°
    2020-12-01 01:34

    When you fetch an object by any relation you want, and replicate after that, all relations you retrieved are also replicated. for example:

    $oldUser = User::with('roles')->find(1);
    $newUser = $oldUser->replicate();
    

提交回复
热议问题