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:

Schema::create('tasks', function (Blueprint $table) {

And a model:

class Tasks extends Model
    use SoftDeletes;
    protected $table = 'tasks';
    protected $fillable = ['task', 'description'];

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


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

$tasks = Tasks::find(1);
$newTask = $tasks->replicate();

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.

Like our articles?
Check out our Laravel online courses!


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

    • 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()){


    • OH I freaking loved this code, it saved me a lot of hassle, Imagine I had more than 50 fields to replicate.
      @Warden, I have a scenario where if any change is done to any field by any user it need to record a new entry into the table with the info who edited and when, so old entry is not to be edited, this is very crucial in finance software that you need to record every single activities…..


Please enter your comment!
Please enter your name here