0

Cloning or Duplicate a database record a headache? not anymore with Laravel.

Laravel provides a very awesome function for this called replicate which will take an Eloquent model and make a copy so you can then make changes and save it.

Here’s an example of how you might use this.
Let’s pretend you have a blog post and you want to make a copy of it to publish again. First grab the original model:

$post = Post::find(1); 

Next, call the replicate method on it:

$newPost = $post->replicate();

Now, you can make any changes you need to the model and then resave it.

$newPost->created_at = Carbon::now();
$newPost->save();

ALSO READ
7 websites you need to use as a developer


All together it would look like this:

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

This replicate method is really helpful for quickly cloning or duplicating a database record.