Jigar
Jigar

Reputation: 3261

Laravel 7 | how can i clone eloquent object and add entry in table with all relationship

I want to clone one product with all its relationship like price, attributes, images etc.

also, price and attribute have another relationship (nested relationships)

is there any easy way I can clone all this with few line of code?

Upvotes: 0

Views: 4226

Answers (1)

Aless55
Aless55

Reputation: 2709

You can use the replicate() function for the model itself. But this wont make a deep copy in terms of creating also the related entries. https://laravel.com/api/7.x/Illuminate/Database/Eloquent/Model.html#method_replicate

You will have to write your own code where you get the related models and replicate them or you can use a package like: https://github.com/Neurony/laravel-duplicate

Upvotes: 2

Related Questions