3 ways to Move Data from One Table to Another Table Laravel

December 3, 2022
Laravel
3 ways to Move Data from One Table to Another Table Laravel

In this tutorial, you will learn how to move data from one table to another table in laravel. We will be discussing 3 ways to move data from one table to another.

We will be using the replicate() laravel eloquent method to duplicate the data and setTable() method to migrate the data to other table.

Lets get started!

Before getting started, let’s find out why we are using replicate() and setTable().

What does replicate() does in laravel?

Replicate() is laravel eloquent build in method, what it simply does is create unsaved copy of current record which we can use.

Note: Important point is that replicate() can copy one row at the time.

$article = Article::find(1);
$newArticle = $article ->replicate();

What does setTable() does in laravel?

If you see the setTable() function in the laravel Model Class of laravel framework, will contain the following code:

 public function setTable($table)
    {
        $this->table = $table;

        return $this;
    }

The $table name we passed to setTable will be set as the table name.

Method 1: Laravel Move One Row To Another Table

We will be using the replicate() and setTable() methods to move one row to another table.

<?php

namespace App\Http\Controllers;

use App\Models\Article;
use Illuminate\Http\Request;

class TestController extends Controller
{
    //
    public function index(Request $request)
    {

        //getting the record want to copy
        $article = Article::find(1);

        //copy them using replicate and setting destination table by setTable()
        $article->replicate()
            ->setTable('old_articles')
            ->save();

        //add following command if you need to remove records from first table
        $article->delete();
    }
}

In the above code, first we are getting the record single record using find(), then we are copying the record using replicate() and then setting the destination table name using setTable and then saving it.

Note: The replicate() only copies from the source table, so if you want to delete the record after copying you can do that using delete() command.

Method 2: Laravel Move Multiple Rows to Another Table

Now for moving multiple rows from one table to another we will be using again replicate() and setTable() methods.

<?php

namespace App\Http\Controllers;

use App\Models\Article;
use Illuminate\Http\Request;

class TestController extends Controller
{
    public function index(Request $request)
    {

        
        Article::select("*")
            ->where('id','>', 300)
            ->each(function ($article) {

                //getting the record one by one that want to be copied
                $newUser = $article->replicate();

                //copy them using replicate and setting destination table by setTable()
                $newUser->setTable('old_articles');
                $newUser->save();

                //add following command if you need to remove records from first table
                $article->delete();
            });
    }
}

In the above code, first we set the condition for the articles we want to fetch using where() and then we are using each() that will give us each article one by one that need to be copied as the replicate() function copy only one row at the time.

Then we are using replicate() to copy the row itself and then setTable() to set the destination table and then we are saving it.

We also have to delete record from the source table, thats why we are using delete() at the end.

Method 3: Laravel Copy Data to Same Table

In this method, we will be copy the data into the same table using replicate() only.

<?php

namespace App\Http\Controllers;

use App\Models\Article;
use Illuminate\Http\Request;

class TestController extends Controller
{

    //
    public function index(Request $request)
    {

        //getting the record want to copy
        $article = Article::find(1);

        //copy them using replicate and saving, since we are not doing setTable means destination table will be same as source table
        $article->replicate()->save();
    }

In the above code, first we using find() to select the row we want to copy, then we using replicate() to copy the row data itself.

Since we are not using the setTable(), in this case the destination table will be same as source table, so the row gets copied into the articlestable itself.

Conclusion:

Hope you have learned today on how to move or copy data from one table to another using 3 different methods in laravel which we have discussed above.

See you in the next one.

Write a Reply or Comment

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


Icon