Laravel Copy Record using Eloquent Replicate Example

By Hardik Savani | March 26, 2021 | Category : Laravel


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

<?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...




Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. 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, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: