laravel clone model code example

Example: Laravel - Clone existing model

<?php  namespace App\Models;  use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model;  class Category extends Model{    use HasFactory;      /**     * Get the comments for the blog post.     */    public function products()    {        return $this->hasMany(Product::class);    }      /**     * Get the comments for the blog post.     */    public function replicateRow()    {       $clone = $this->replicate();       $clone->push();                foreach($this->products as $product)       {           $clone->products()->create($product->toArray());       }         $clone->save();    }}

Tags:

Php Example