Eloquent: how to make a copy of a row

Today I want to tell you about one “hidden” Laravel feature which is in the system but not in documentation – replicate. This function allows you to make a copy of a row in the fastest way possible.

To start with, we are going to have a table:

And a model:

In the database we have a task which looks like this:


And then we run the following code to duplicate a single row:

We should see that our database row was replicated with new id and updated timestamps:


And that’s it! With a few lines of code you can replicate a lot of data. This method works with loops too, so you can use this method to duplicate all of our entries.

Want more tips and articles like this?
Subscribe to our weekly newsletter - comes out every Thursday!
Join 1200+ Laravel professionals who read the issue weekly.

3 thoughts on “Eloquent: how to make a copy of a row

    1. For that it was a bit complicated. I couldn’t get it simply working, so I had to improvize with hand written relationship replication

    2. to copy relationships, you need to loop through it and replicate.

      foreach ($tasks->>getRelations() as $relation => $entries){
      foreach($entries as $entry){
      $e = $entry->replicate();
      if ($e->push()){


Leave a Reply

Your email address will not be published. Required fields are marked *