pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

      • <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 class="ceywxkv" 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 關系時,如何更新 Has

        How to update a HasOne relationship when a HasMany relationship also exists with the same model?(當同一模型也存在 HasMany 關系時,如何更新 HasOne 關系?)

              <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>
              • <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 class="83dvyx8" id='OcxcK'><tfoot id='OcxcK'></tfoot><dl id='OcxcK'><fieldset id='OcxcK'></fieldset></dl></div>
              • <tfoot id='OcxcK'></tfoot>

                    <tbody id='OcxcK'></tbody>
                  本文介紹了當同一模型也存在 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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環)
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數)
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“localhost的訪問被拒絕)
                    <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 class="ynua8mo" 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>
                            主站蜘蛛池模板: 北京三友信电子科技有限公司-ETC高速自动栏杆机|ETC机柜|激光车辆轮廓测量仪|嵌入式车道控制器 | 河南中整光饰机械有限公司-抛光机,去毛刺抛光机,精密镜面抛光机,全自动抛光机械设备 | 沈飞防静电地板__机房地板-深圳市沈飞防静电设备有限公司 | 电渗析,废酸回收,双极膜-山东天维膜技术有限公司 | 气体检测仪-氢气检测仪-可燃气体传感器-恶臭电子鼻-深国安电子 | 创富网-B2B网站|供求信息网|b2b平台|专业电子商务网站 | 二手色谱仪器,十万分之一分析天平,蒸发光检测器,电位滴定仪-湖北捷岛科学仪器有限公司 | 物和码官网,物和码,免费一物一码数字化营销SaaS平台 | 高防护蠕动泵-多通道灌装系统-高防护蠕动泵-www.bjhuiyufluid.com慧宇伟业(北京)流体设备有限公司 | 大倾角皮带机-皮带输送机-螺旋输送机-矿用皮带输送机价格厂家-河南坤威机械 | 佛山商标注册_商标注册代理|专利注册申请_商标注册公司_鸿邦知识产权 | 动物麻醉机-数显脑立体定位仪-北京易则佳科技有限公司 | 成都热收缩包装机_袖口式膜包机_高速塑封机价格_全自动封切机器_大型套膜机厂家 | 液晶拼接屏厂家_拼接屏品牌_拼接屏价格_监控大屏—北京维康 | 深圳货架厂_仓库货架公司_重型仓储货架_线棒货架批发-深圳市诺普泰仓储设备有限公司 | 德州网站开发定制-小程序开发制作-APP软件开发-「两山开发」 | 非标压力容器_碳钢储罐_不锈钢_搪玻璃反应釜厂家-山东首丰智能环保装备有限公司 | 防水试验机_防水测试设备_防水试验装置_淋雨试验箱-广州岳信试验设备有限公司 | 臭氧实验装置_实验室臭氧发生器-北京同林臭氧装置网 | 超声波焊接机,振动摩擦焊接机,激光塑料焊接机,超声波焊接模具工装-德召尼克(常州)焊接科技有限公司 | 钢化玻璃膜|手机钢化膜|钢化膜厂家|手机保护膜-【东莞市大象电子科技有限公司】 | 标策网-专注公司商业知识服务、助力企业发展 | 泰国专线_泰国物流专线_广州到泰国物流公司-泰廊曼国际 | 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | 智能化的检漏仪_气密性测试仪_流量测试仪_流阻阻力测试仪_呼吸管快速检漏仪_连接器防水测试仪_车载镜头测试仪_奥图自动化科技 | 逗网红-抖音网红-快手网红-各大平台网红物品导航 | 全国冰箱|空调|洗衣机|热水器|燃气灶维修服务平台-百修家电 | 冷藏车-东风吸污车-纯电动环卫车-污水净化车-应急特勤保障车-程力专汽厂家-程力专用汽车股份有限公司销售二十一分公司 | 防勒索软件_数据防泄密_Trellix(原McAfee)核心代理商_Trellix(原Fireeye)售后-广州文智信息科技有限公司 | 东莞市超赞电子科技有限公司 全系列直插/贴片铝电解电容,电解电容,电容器 | 卫生人才网-中国专业的医疗卫生医学人才网招聘网站! | 河南mpp电力管_mpp电力管生产厂家_mpp电力电缆保护管价格 - 河南晨翀实业 | 山东石英砂过滤器,除氟过滤器「价格低」-淄博胜达水处理 | 工业胀紧套_万向节联轴器_链条-规格齐全-型号选购-非标订做-厂家批发价格-上海乙谛精密机械有限公司 | 不锈钢水箱生产厂家_消防水箱生产厂家-河南联固供水设备有限公司 | Safety light curtain|Belt Sway Switches|Pull Rope Switch|ultrasonic flaw detector-Shandong Zhuoxin Machinery Co., Ltd | 金属切削液-脱水防锈油-电火花机油-抗磨液压油-深圳市雨辰宏业科技发展有限公司 | 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 消泡剂_水处理消泡剂_切削液消泡剂_涂料消泡剂_有机硅消泡剂_广州中万新材料生产厂家 | 异噻唑啉酮-均三嗪-三丹油-1227-中北杀菌剂厂家 | 洗地机_全自动洗地机_手推式洗地机【上海滢皓环保】 |