Laravel Move Data from One Table to Another Table Example
Hello,
This tutorial shows you laravel move data from one table to another. This post will give you a simple example of laravel migrate data from one table to another table. I would like to share with you how to move data one table to another table in laravel. you will learn how to migrate data one table to another table in laravel.
Laravel provides replicate() eloquent method to move records from one table to another table. we will use replicate() with setTable() method to migrate data one table to another. I will give you the following three examples:
you can easily use replicate() from laravel 8 version.
Example 1: Laravel Move One Row to Another Table
Here, we will use 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('inactive_users')
->save();
}
}
Example 2: Laravel Move Multiple Row to Another Table
Here, we will use replicate() and setTable() method to move multiple records 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::select("*")
->where('last_login','<', now()->subYear())
->each(function ($user) {
$newUser = $user->replicate();
$newUser->setTable('inactive_users');
$newUser->save();
$user->delete();
});
}
}
Example 3: Laravel Copy Data to Same Table
Here, we will use replicate() and save() method to copy data on same setTable.
<?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);
}
}
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Eloquent without() and withOnly() Method Example
- Laravel Eloquent whereHas() Condition Example
- Laravel Eloquent Group By with Month and Year Example
- Laravel Eloquent Left Join Where Null Condition Example
- Laravel Replicate Model with Relationships Example
- Laravel Copy Record using Eloquent Replicate Example
- Laravel Eloquent Sum Multiple Columns Example
- Laravel Eloquent firstWhere() Example
- Laravel Eloquent whereNotBetween() Query Example
- Laravel Eloquent whereBetween() Query Example
- Laravel Eloquent Order By Query Example
- Laravel Eloquent orderByRaw() Query Example
- Laravel Many to Many Eloquent Relationship Tutorial
- Laravel Has Many Through Eloquent Relationship Tutorial