Laravel Eloquent ORM replicate

前端 未结 4 1506
悲哀的现实
悲哀的现实 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:55

    Try using attach to create the relationship:

    foreach($product->options as $option){
        $new_option = $option->replicate();
        $new_option->save();
        $new_option_id = $new_option->id;
        $new_product->options()->attach($new_option_id);
    }
    

提交回复
热议问题