Php 拉维尔雄辩

Php 拉维尔雄辩,php,laravel,eloquent,clone,relationship,Php,Laravel,Eloquent,Clone,Relationship,我在复制一个模型和所有关系时遇到问题 数据库结构如下: Table1: products id name Table2: product_options id product_id option Table3: categories id name Pivot table: product_categories product_id category_id 关系包括: 产品有许多产品选项 属于任何类别的产品(通过产品类别) 我想克隆一个具有所有关系的产品。目前我的代码如下: $pro

我在复制一个模型和所有关系时遇到问题

数据库结构如下:

Table1: products
id
name

Table2: product_options
id
product_id
option

Table3: categories
id
name

Pivot table: product_categories
product_id
category_id
关系包括:

  • 产品有许多产品选项
  • 属于任何类别的产品(通过产品类别)
我想克隆一个具有所有关系的产品。目前我的代码如下:

$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();
}
但这不起作用(关系未克隆-目前我只是尝试克隆产品选项)。

尝试使用创建关系:

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);
}

这段代码对我很有用:

$model = User::find($id);

$model->load('invoices');

$newModel = $model->replicate();
$newModel->push();

foreach($model->getRelations() as $relation => $items){
    foreach($items as $item){
        unset($item->id);
        $newModel->{$relation}()->create($item->toArray());
    }
}
回答如下:

这个答案(同样的问题)也很好用

//copy attributes from original model
$newRecord = $original->replicate();
// Reset any fields needed to connect to another parent, etc
$newRecord->some_id = $otherParent->id;
//save model before you recreate relations (so it has an id)
$newRecord->push();
//reset relations on EXISTING MODEL (this way you can control which ones will be loaded
$original->relations = [];
//load relations on EXISTING MODEL
$original->load('somerelationship', 'anotherrelationship');
//re-sync the child relationships
$relations = $original->getRelations();
foreach ($relations as $relation) {
    foreach ($relation as $relationRecord) {
        $newRelationship = $relationRecord->replicate();
        $newRelationship->some_parent_id = $newRecord->id;
        $newRelationship->push();
    }
}
从这里开始:

根据我的经验,该代码适用于多对多关系。

这在5.5版上运行良好。
$product = Product::with('options')->find($id);
$new_product = $product->replicate();
$new_product->{attribute} = {value};
$new_product->push();

$new_product->options()->saveMany($product->options);
图像、媒体是一个关系名称

$event = Events::with('image','media')->find($event_id);
            if($event){
                $newevent = $event->replicate();
                $newevent->push();
                 foreach ($newevent->getRelations() as $relation => $entries)
                 {
                    foreach($entries as $entry)
                    {
                        $e = $entry->replicate();
                        if ($e->push())
                        {
                            $newevent->{$relation}()->save($e);
                        }
                    }

                }                                

            }

此答案:此答案:为meCan工作,请将您的评论移至答案?您应该解释您的答案,以便询问者(以及其他找到答案的人)了解此代码的功能。使用saveMany这种方式不会添加新记录,只会用新模型的值覆盖外键