Posts Learn Snippets Categories Tags About
/

How to Replicate Laravel Model

Learn how to easily replicate or clone your Laravel model

Created on Jul 12, 2021

15 views

Sometimes you might need to replicate or clone your model and to do that you can make use of the "replicate" method available by the "Model" instance itself.

Imagine you have a "Post" model and you want to replicate it, you can do so like below.
// retrieve the first post
$post = Post::first();

// replicate the first post and save it as a new post
$post->replicate()->save();

Now when you have a look on the database, you will have a new record with the same exact content as the one you have replicated but with a different "id" (identifier) since it's unique.

Created by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.