Posts Learn Components Snippets Categories Tags Tools About
/

How to Replicate Laravel Model

Learn how to easily replicate or clone your Laravel model

Created on Jul 12, 2021

358 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.

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

Load comments for How to Replicate Laravel Model

)