Hi,
Now, let’s see article of laravel copy row using replicate. In this article, we will implement a laravel duplicate record. this example will help you laravel eloquent copy record example. you will learn laravel eloquent replicate example.
Sometime we need to create duplicate record on same database table. at that time if we create that record manually then it can take time and can generate error. as specially issue when we have 10 or 20 fields on that table. so laravel eloquent provide replicate() that can help to create duplicate entry on same table.
you can easily use replicate() with laravel 8 version.
Let’s see two simple example how to create duplicate record using replicate().
Example 1
<?php
namespace App\Http\Controllers;
use App\Models\Product;
class SignaturePadController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$product = Product::find(2);
$newProduct = $product->replicate()->save();
dd($newProduct);
}
}
Output:
Example 2: Update Some Fields
Read Also: Delete All Records from Table in Laravel Eloquent
<?php
namespace App\Http\Controllers;
use App\Models\Product;
class SignaturePadController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$product = Product::find(2);
$newProduct = $product->replicate();
$newProduct->amount = 100;
$newProduct->save();
dd($newProduct);
}
}
Output:
i hope it can help you…