Laravel Copy Record using Eloquent Replicate Example

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Hi Dev’s, Today in this I will show you Copy Record using Eloquent Replicate in laravel. Now, let’s start a new article of laravel copy row by using replicate in laravel. Now in this article, i will implement a laravel duplicate record. So I hope this example will help you on laravel eloquent copy record example. Now you can learn Laravel Copy Record using Eloquent.

Sometime we have to create more duplicate record on same database table. At that time if we want to create that all record manually then it will take long time and also can generate error. as specially issue when we need to 10 or 20 fields on one table. So on laravel eloquent provide us replicate() function that can help us to create duplicate entry on same database table.

Also we can easily use replicate() function with laravel 8 version application.

Let’s start and see two very simple example of how to create duplicate record by 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);
    }
}
Laravel Copy Record using Eloquent

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);
    }
}
Laravel Copy Record using Eloquent

Read Also : proc_open(): fork failed – Cannot allocate memory laravel in ubuntu

Thanks for read. I hope it help you. For more you can follow us on facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →