Laravel Move Data From One Table To Another Table Example

Reading Time: 4 minutes
1,034 Views

Inside this article we will see the concept i.e Laravel Move Data From One Table To Another Table. Article contains the classified information about Copy data from one table to another table with Laravel code.

Here, we will see three different methods by which you can copy or more data From one table to another table. Tutorial is super easy to understand and implement it in your code as well.

We will see the concept of these methods –

  • replicate() method
  • setTable() method
  • each() method
  • save() method

Read More: How To Check a Key Exists in JavaScript Object?

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Method #1: Laravel Move One Row to Another Table

Here, you will see the concept of replicate() and setTable() method to move one record to another table.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $user = User::find(1);
  
        $user->replicate()
             ->setTable('backup_users')
             ->save();
    }
}

Inside this above code, you can see we are creating a copy of User ID 1 from users table to backup_users table.

Read More: Upload File To Remote Server Through FTP Using PHP

Method #2: Laravel Move Multiple Row to Another Table

Again, we will use the concept of replicate() and setTable() method to move multiple records to another table. But this will be done by using each method like a loop to iterate over each record.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        User::select("*")
                ->where('last_login','<', now()->subYear())
                ->each(function ($user) {
                        $newUser = $user->replicate();
                        $newUser->setTable('backup_users');
                        $newUser->save();
  
                        $user->delete();
                  });
    }
}

Inside above code, you can see we are selecting all users. Then by using each loop method concept one by one we are creating a copy of each user’s row into a table called backup_users.

Method #3: Laravel Copy Data to Same Table

Here, we will use only the concept of replicate() and save() method to copy data on same table.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Product;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $product = Product::find(2);
    
        $newProduct = $product->replicate()->save();
    
        dd($newProduct);
    }
}

Inside above code, we are selecting a product of ID 2 and then we are creating a copy of it using replicate() method and save into same table.

Read More: Password Generator with Strength Checker in JavaScript

We hope this article helped you to learn Laravel Move Data From One Table To Another Table Example Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more