<legend id='OcxcK'><style id='OcxcK'><dir id='OcxcK'><q id='OcxcK'></q></dir></style></legend>

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

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

      3. 当同一模型也存在 HasMany 关系时,如何更新 Has

        时间:2023-09-22

        <tfoot id='3Qxi3'></tfoot>

          <small id='3Qxi3'></small><noframes id='3Qxi3'>

              <tbody id='3Qxi3'></tbody>
            • <bdo id='3Qxi3'></bdo><ul id='3Qxi3'></ul>

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

              1. <legend id='3Qxi3'><style id='3Qxi3'><dir id='3Qxi3'><q id='3Qxi3'></q></dir></style></legend>
                  本文介绍了当同一模型也存在 HasMany 关系时,如何更新 HasOne 关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试在 Eloquent 中定义相同的两个模型之间的 HasMany 和 HasOne 关系.

                  我的Organization类有很多Contact:

                  公共函数contacts(){返回 $this->hasMany(Contact::class);}

                  同样,我的 Contact 类反映了这种关系:

                  公共函数组织(){返回 $this->belongsTo(Organization::class);}

                  而且,每个组织都有一个主要"联系人.我正在使用表列 organizations.primary_contact_id 来确定哪个:

                  公共函数primaryContact(){返回 $this->hasOne(Contact::class, 'id', 'primary_contact_id');}

                  从这里开始,我被卡住了.Contact 中的反向关系已经存在,所以我写了另一个我认为可以解决问题的函数,计算如果我更新了父表中的值,Eloquent 自然会在contacts 表中获取相应的记录,因为我定义了关系:

                  /*** @param AppContact*/公共函数 setPrimaryContact($contact){$this->primary_contact_id = $contact->id;$this->save;}

                  但它没有:

                  <预><代码>>>>$org = 组织::查找(17)=>应用组织 {#2923编号:17,name: "测试组织",primary_contact_id: 33,}>>>$alice= $org->primaryContact=>应用联系{#2938编号:33,组织 ID:17,fname: "爱丽丝",lname: "方丈",}>>>$bob = 联系人::查找(34)=>应用联系{#2939编号:34,组织 ID:17,fname: "鲍勃",lname: "面包师",}>>>$org->setPrimaryContact($bob)=>空值>>>$org=>应用组织 {#2923编号:17,name: "测试组织",primary_contact_id: 34,主要联系人:AppContact {#2938编号:33,组织 ID:17,fname: "爱丽丝",lname: "方丈",},}

                  您可以看到 setPrimaryContact($bob) 执行得很好,因为 primary_contact_id 已更新为 Bob 的 id,但是 primaryContact 仍然列出 Alice.

                  为什么 primaryContact 没有返回正确的对象?

                  解决方案

                  • 您的 setPrimaryContact 方法不会更新您的表,因为您调用的是 $this->save,而不是 $this->save(), save 是一个方法
                  • $org->setPrimaryContact($bob)之后,你应该调用$org->primaryContact->refresh() 以获取更新的记录.

                  I'm trying to define both a HasMany and HasOne relationship between the same two models in Eloquent.

                  My Organization class has many Contacts:

                  public function contacts()
                  {
                      return $this->hasMany(Contact::class);
                  }
                  

                  And likewise, my Contact class reflects this relationship:

                  public function organization()
                  {
                      return $this->belongsTo(Organization::class);
                  }
                  

                  But also, each Organization has exactly one "primary" Contact. I am using a table column organizations.primary_contact_id to identify which one:

                  public function primaryContact()
                  {
                      return $this->hasOne(Contact::class, 'id', 'primary_contact_id');
                  }
                  

                  From here, I'm stuck. The reverse relationship in Contact already exists, so I wrote another function I thought would do the trick, figuring if I updated the value in the parent table, Eloquent would naturally fetch the corresponding record in the contacts table since I defined the relationship:

                  /**
                   * @param AppContact
                   */
                  public function setPrimaryContact($contact)
                  {
                      $this->primary_contact_id = $contact->id;
                      $this->save;
                  }
                  

                  But it doesn't:

                  >>> $org = Organization::find(17)
                  => AppOrganization {#2923
                       id: 17,
                       name: "Test Org",
                       primary_contact_id: 33,
                     }
                  >>> $alice= $org->primaryContact
                  => AppContact {#2938
                       id: 33,
                       organization_id: 17,
                       fname: "Alice",
                       lname: "Abbot",
                     }
                  >>> $bob = Contact::find(34)
                  => AppContact {#2939
                       id: 34,
                       organization_id: 17,
                       fname: "Bob",
                       lname: "Baker",
                     }
                  >>> $org->setPrimaryContact($bob)
                  => null
                  >>> $org
                  => AppOrganization {#2923
                       id: 17,
                       name: "Test Org",
                       primary_contact_id: 34,
                       primaryContact: AppContact {#2938
                         id: 33,
                         organization_id: 17,
                         fname: "Alice",
                         lname: "Abbot",
                       },
                     }
                  

                  You can see setPrimaryContact($bob) executed fine, as primary_contact_id got updated to Bob's id, but primaryContact still lists Alice.

                  Why is primaryContact not returning the correct object?

                  解决方案

                  • Your setPrimaryContact method won't update your table, because you call $this->save, not $this->save(), save is a method
                  • After $org->setPrimaryContact($bob), you should call $org-> primaryContact->refresh() to get the updated record.

                  这篇关于当同一模型也存在 HasMany 关系时,如何更新 HasOne 关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                    <tbody id='4ikJU'></tbody>

                    <bdo id='4ikJU'></bdo><ul id='4ikJU'></ul>

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

                          <small id='4ikJU'></small><noframes id='4ikJU'>

                          <tfoot id='4ikJU'></tfoot>