Clone an Eloquent object including all relationships?

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

    This is in laravel 5.8, havent tried in older version

    //# this will clone $eloquent and asign all $eloquent->$withoutProperties = null
    $cloned = $eloquent->cloneWithout(Array $withoutProperties)
    

    edit, just today 7 April 2019 laravel 5.8.10 launched

    can use replicate now

    $post = Post::find(1);
    $newPost = $post->replicate();
    $newPost->save();
    

提交回复
热议问题