【发布时间】:2016-07-01 11:32:07
【问题描述】:
我在使用所有关系复制我的一个模型时遇到问题。
数据库结构如下:
Table1: products
id
name
Table2: product_options
id
product_id
option
Table3: categories
id
name
Pivot table: product_categories
product_id
category_id
关系是:
- product hasMany product_options
- product belongsToMany 类别(通过 product_categories)
我想克隆一个包含所有关系的产品。目前这是我的代码:
$product = Product::with('options')->find($id);
$new_product = $product->replicate();
$new_product->push();
foreach($product->options as $option){
$new_option = $option->replicate();
$new_option->product_id = $new_product->id;
$new_option->push();
}
但这不起作用(关系未克隆 - 目前我只是尝试克隆 product_options)。
【问题讨论】:
-
您能否将您的评论移至答案?
标签: php laravel eloquent clone relationship