Laravel - Clone existing 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; public function products() { return $this->hasMany(Product::class); } public function replicateRow() { $clone = $this->replicate(); $clone->push(); foreach($this->products as $product) { $clone->products()->create($product->toArray()); } $clone->save(); }}