ItSolutionStuff.com

Laravel Copy Record using Eloquent Replicate Example

By Hardik Savani • November 5, 2023
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

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Eloquent take() and skip() Query Example

Read Now →

Laravel Eloquent whereNull() Query Example

Read Now →

Laravel Eloquent whereBetween() Query Example

Read Now →

Laravel Eloquent selectRaw() Query Example

Read Now →

Laravel Eloquent Order By Query Example

Read Now →

Laravel Eloquent orderByRaw() Query Example

Read Now →

Laravel Eloquent whereRaw Condition Example

Read Now →

Laravel Eloquent Where Query Examples

Read Now →

Laravel Collection Merge | How to Merge Two Eloquent Collection?

Read Now →

Laravel Eloquent Relationships Tutorial From Scratch

Read Now →

Laravel Many to Many Eloquent Relationship Tutorial

Read Now →