<small id='fwlhE'></small><noframes id='fwlhE'>

    1. <tfoot id='fwlhE'></tfoot>
      <i id='fwlhE'><tr id='fwlhE'><dt id='fwlhE'><q id='fwlhE'><span id='fwlhE'><b id='fwlhE'><form id='fwlhE'><ins id='fwlhE'></ins><ul id='fwlhE'></ul><sub id='fwlhE'></sub></form><legend id='fwlhE'></legend><bdo id='fwlhE'><pre id='fwlhE'><center id='fwlhE'></center></pre></bdo></b><th id='fwlhE'></th></span></q></dt></tr></i><div id='fwlhE'><tfoot id='fwlhE'></tfoot><dl id='fwlhE'><fieldset id='fwlhE'></fieldset></dl></div>

      <legend id='fwlhE'><style id='fwlhE'><dir id='fwlhE'><q id='fwlhE'></q></dir></style></legend>
      • <bdo id='fwlhE'></bdo><ul id='fwlhE'></ul>

      Laravel Eloquent ORM 复制

      时间:2023-09-24
        <tbody id='POhZM'></tbody>
    2. <i id='POhZM'><tr id='POhZM'><dt id='POhZM'><q id='POhZM'><span id='POhZM'><b id='POhZM'><form id='POhZM'><ins id='POhZM'></ins><ul id='POhZM'></ul><sub id='POhZM'></sub></form><legend id='POhZM'></legend><bdo id='POhZM'><pre id='POhZM'><center id='POhZM'></center></pre></bdo></b><th id='POhZM'></th></span></q></dt></tr></i><div id='POhZM'><tfoot id='POhZM'></tfoot><dl id='POhZM'><fieldset id='POhZM'></fieldset></dl></div>

      <small id='POhZM'></small><noframes id='POhZM'>

          <bdo id='POhZM'></bdo><ul id='POhZM'></ul>
          <legend id='POhZM'><style id='POhZM'><dir id='POhZM'><q id='POhZM'></q></dir></style></legend>

          <tfoot id='POhZM'></tfoot>

                本文介绍了Laravel Eloquent ORM 复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我在使用所有关系复制我的模型之一时遇到问题.

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

                数据库结构如下:

                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_categories)

                我想克隆一个具有所有关系的产品.目前这是我的代码:

                I would like to clone a product with all the relationships. Currently here is my code:

                $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).

                But this does not works (the relationships are not cloned - currently I just tried to clone the product_options).

                推荐答案

                这段代码,对我有用:

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

                从这里回答:克隆一个包含所有关系的 Eloquent 对象?

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

                This answer (same question), also works fine too.

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

                从这里:克隆一个包含所有关系的 Eloquent 对象?

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

                The code works fine for many to many relationships in my experience.

                这篇关于Laravel Eloquent ORM 复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Laravel,如何忽略访问器 下一篇:过滤结果时是否有更短的方法来设置查询?

                相关文章

                最新文章

                  <bdo id='zTTWl'></bdo><ul id='zTTWl'></ul>
              1. <small id='zTTWl'></small><noframes id='zTTWl'>

                <i id='zTTWl'><tr id='zTTWl'><dt id='zTTWl'><q id='zTTWl'><span id='zTTWl'><b id='zTTWl'><form id='zTTWl'><ins id='zTTWl'></ins><ul id='zTTWl'></ul><sub id='zTTWl'></sub></form><legend id='zTTWl'></legend><bdo id='zTTWl'><pre id='zTTWl'><center id='zTTWl'></center></pre></bdo></b><th id='zTTWl'></th></span></q></dt></tr></i><div id='zTTWl'><tfoot id='zTTWl'></tfoot><dl id='zTTWl'><fieldset id='zTTWl'></fieldset></dl></div>
                <legend id='zTTWl'><style id='zTTWl'><dir id='zTTWl'><q id='zTTWl'></q></dir></style></legend>

              2. <tfoot id='zTTWl'></tfoot>