Laravel Copy Record using Eloquent Replicate Example
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
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 take() and skip() Query Example
- Laravel Eloquent whereNull() Query Example
- Laravel Eloquent whereBetween() Query Example
- Laravel Eloquent selectRaw() Query Example
- Laravel Eloquent Order By Query Example
- Laravel Eloquent orderByRaw() Query Example
- Laravel Eloquent whereRaw Condition Example
- Laravel Eloquent Where Query Examples
- Laravel Collection Merge | How to Merge Two Eloquent Collection?
- Laravel Eloquent Relationships Tutorial From Scratch
- Laravel Many to Many Eloquent Relationship Tutorial