Laravel Eloquent ORM replicate

前端 未结 4 1504
悲哀的现实
悲哀的现实 2020-12-18 22:19

I have a problem with replicating one of my models with all the relationships.

The database structure is as follows:

Table1: products
id
name

Table2         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-18 22:32

    $product = Product::with('options')->find($id);
    $new_product = $product->replicate();
    $new_product->{attribute} = {value};
    $new_product->push();
    
    $new_product->options()->saveMany($product->options);
    

提交回复
热议问题