Home / Snippets / How to Replicate Laravel Model
How to Replicate Laravel Model cover

How to Replicate Laravel Model

385

3 years ago

0 comments

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.
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

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

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this