Clone an Eloquent object including all relationships?

后端 未结 10 699
我寻月下人不归
我寻月下人不归 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:53

    If you have a collection named $user, using the code bellow, it creates a new Collection identical from the old one, including all the relations:

    $new_user = new \Illuminate\Database\Eloquent\Collection ( $user->all() );
    

    this code is for laravel 5.

提交回复
热议问题