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

    <bdo id='B1yYx'></bdo><ul id='B1yYx'></ul>
    <i id='B1yYx'><tr id='B1yYx'><dt id='B1yYx'><q id='B1yYx'><span id='B1yYx'><b id='B1yYx'><form id='B1yYx'><ins id='B1yYx'></ins><ul id='B1yYx'></ul><sub id='B1yYx'></sub></form><legend id='B1yYx'></legend><bdo id='B1yYx'><pre id='B1yYx'><center id='B1yYx'></center></pre></bdo></b><th id='B1yYx'></th></span></q></dt></tr></i><div class="oo4ocqa" id='B1yYx'><tfoot id='B1yYx'></tfoot><dl id='B1yYx'><fieldset id='B1yYx'></fieldset></dl></div>
  • <tfoot id='B1yYx'></tfoot>

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

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

        laravel 5.x 中的 hasMany 與 BeingToMany

        hasMany vs belongsToMany in laravel 5.x(laravel 5.x 中的 hasMany 與 BeingToMany)

          1. <small id='vDeE6'></small><noframes id='vDeE6'>

              <tbody id='vDeE6'></tbody>

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

                  <tfoot id='vDeE6'></tfoot>
                  本文介紹了laravel 5.x 中的 hasMany 與 BeingToMany的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我很好奇為什么 hasMany 的 Eloquent 關系與 belongsToMany 的簽名不同.特別是自定義連接表名稱——對于給定 Comment 屬于許多 Role 的系統(tǒng),并且給定的 Role 將有許多 Comments,我想將關系存儲在一個名為 my_custom_join_table 的表中,并將鍵設置為 comment_keyrole_key>.

                  I'm curious why the Eloquent relationship for hasMany has a different signature than for belongsToMany. Specifically the custom join table name-- for a system where a given Comment belongs to many Roles, and a given Role would have many Comments, I want to store the relationship in a table called my_custom_join_table and have the keys set up as comment_key and role_key.

                  return $this->belongsToMany('AppRole', 'my_custom_join_table', 'comment_key', 'role_key'); // works
                  

                  但反過來,我無法定義該自定義表(至少文檔沒有提及):

                  But on the inverse, I can't define that custom table (at least the docs don't mention it):

                  return $this->hasMany('AppComment', 'comment_key', 'role_key');
                  

                  如果我有一個 hasMany CommentsRole 對象,但我使用非標準表名來存儲該關系,為什么可以我以一種方式使用這個非標準表而不是另一種方式?

                  If I have a Role object that hasMany Comments, but I use a non-standard table name to store that relationship, why can I use this non-standard table going one way but not the other?

                  推薦答案

                  hasMany 用于 一對多關系,而 belongsToMany 指的是 多對多關系.它們都是不同的關系類型,每種都需要不同的數(shù)據(jù)庫結構 - 因此它們采用不同的參數(shù).

                  hasMany is used in a One To Many relationship while belongsToMany refers to a Many To Many relationship. They are both distinct relationship types and each require a different database structure - thus they take different parameters.

                  關鍵區(qū)別在于,在一對多關系中,您只需要與相關模型對應的兩個數(shù)據(jù)庫表.這是因為對關系的引用存儲在擁有模型的表本身中.例如,您可能有一個 Country 模型和一個 City 模型.一個國家有許多城市.但是,每個城市僅存在于一個國家/地區(qū).因此,您可以將該國家/地區(qū)存儲在 City 模型本身(作為 country_id 或類似的東西).

                  The key difference is that in a One To Many relationship, you only need the two database tables that correspond to the related models. This is because the reference to the relation is stored on the owned model's table itself. For instance, you might have a Country model and a City model. A Country has many cities. However, each City only exists in one country. Therefore, you would store that country on the City model itself (as country_id or something like that).

                  但是,多對多關系需要第三個??數(shù)據(jù)庫表,稱為數(shù)據(jù)透視表.數(shù)據(jù)透視表存儲對兩個模型的引用,您可以將其聲明為關系聲明中的第二個參數(shù).例如,假設您有 City 模型和 Car 模型.你想要一個關系來顯示人們在每個城市駕駛的汽車類型.好吧,在一個城市里,人們會駕駛許多 種不同類型的汽車.但是,如果您查看一種汽車類型,您也會知道它可以在許多不同的城市中行駛.因此,不可能在任一模型上存儲 city_idcar_id,因為每個模型都有多個.因此,您將這些引用放入數(shù)據(jù)透視表中.

                  However, a Many To Many relationship requires a third database table, called a pivot table. The pivot table stores references to both the models and you can declare it as a second parameter in the relationship declaration. For example, imagine you have your City model and you also have a Car model. You want a relationship to show the types of cars people drive in each city. Well, in one city people will drive many different types of car. However, if you look at one car type you will also know that it can be driven in many different cities. Therefore it would be impossible to store a city_id or a car_id on either model because each would have more than one. Therefore, you put those references in the pivot table.

                  根據(jù)經(jīng)驗,如果您使用 belongsToMany 關系,它只能與另一個 belongsToMany 關系配對,這意味著您有第三個數(shù)據(jù)透視表.如果您使用 hasMany 關系,它可以belongsTo 關系配對,并且不需要額外的數(shù)據(jù)庫表.

                  As a rule of thumb, if you use a belongsToMany relationship, it can only be paired with another belongsToMany relationship and means that you have a third pivot table. If you use a hasMany relationship, it can only be paired with a belongsTo relationship and no extra database tables are required.

                  在您的示例中,您只需要將逆關系轉換為 belongsToMany 并再次添加您的自定義表,以及外鍵和本地鍵(反轉其他模型的順序).

                  In your example, you just need to make the inverse relation into a belongsToMany and add your custom table again, along with the foreign and local keys (reversing the order from the other model).

                  這篇關于laravel 5.x 中的 hasMany 與 BeingToMany的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環(huán))
                  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 個參數(shù))
                  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@“l(fā)ocalhost的訪問被拒絕)
                      <bdo id='pPWLM'></bdo><ul id='pPWLM'></ul>

                        <i id='pPWLM'><tr id='pPWLM'><dt id='pPWLM'><q id='pPWLM'><span id='pPWLM'><b id='pPWLM'><form id='pPWLM'><ins id='pPWLM'></ins><ul id='pPWLM'></ul><sub id='pPWLM'></sub></form><legend id='pPWLM'></legend><bdo id='pPWLM'><pre id='pPWLM'><center id='pPWLM'></center></pre></bdo></b><th id='pPWLM'></th></span></q></dt></tr></i><div class="0o2cs2o" id='pPWLM'><tfoot id='pPWLM'></tfoot><dl id='pPWLM'><fieldset id='pPWLM'></fieldset></dl></div>
                          <tbody id='pPWLM'></tbody>

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

                          • <tfoot id='pPWLM'></tfoot>

                          • <legend id='pPWLM'><style id='pPWLM'><dir id='pPWLM'><q id='pPWLM'></q></dir></style></legend>
                          • 主站蜘蛛池模板: HDPE土工膜,复合土工膜,防渗膜价格,土工膜厂家-山东新路通工程材料有限公司 | 青州开防盗门锁-配汽车芯片钥匙-保险箱钥匙-吉祥修锁店 | 电液推杆生产厂家|电动推杆|液压推杆-扬州唯升机械有限公司 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 杜康白酒加盟_杜康酒代理_杜康酒招商加盟官网_杜康酒厂加盟总代理—杜康酒神全国运营中心 | 滤芯,过滤器,滤油机,贺德克滤芯,精密滤芯_新乡市宇清流体净化技术有限公司 | 海鲜池-专注海鲜鱼缸、移动海鲜缸、饭店鱼缸设计定做-日晟水族厂家 | 一体化净水器_一体化净水设备_一体化水处理设备-江苏旭浩鑫环保科技有限公司 | ORP控制器_ORP电极价格-上优泰百科| 干粉砂浆设备-干粉砂浆生产线-干混-石膏-保温砂浆设备生产线-腻子粉设备厂家-国恒机械 | 微水泥_硅藻泥_艺术涂料_艺术漆_艺术漆加盟-青岛泥之韵环保壁材 武汉EPS线条_EPS装饰线条_EPS构件_湖北博欧EPS线条厂家 | X光检测仪_食品金属异物检测机_X射线检测设备_微现检测 | 冷油器,取样冷却器,热力除氧器-连云港振辉机械设备有限公司 | 广东燎了网络科技有限公司官网-网站建设-珠海网络推广-高端营销型外贸网站建设-珠海专业h5建站公司「了了网」 | RTO换向阀_VOC高温阀门_加热炉切断阀_双偏心软密封蝶阀_煤气蝶阀_提升阀-湖北霍科德阀门有限公司 | 聚丙烯酰胺_厂家_价格-河南唐达净水材料有限公司 | 广州中央空调回收,二手中央空调回收,旧空调回收,制冷设备回收,冷气机组回收公司-广州益夫制冷设备回收公司 | 沥青车辙成型机-车托式混凝土取芯机-混凝土塑料试模|鑫高仪器 | 郑州水质检测中心_井水检测_河南废气检测_河南中环嘉创检测 | 厂房出租_厂房出售_产业园区招商_工业地产&nbsp;-&nbsp;中工招商网 | 釜溪印象网络 - Powered by Discuz! | 聚氨酯催化剂K15,延迟催化剂SA-1,叔胺延迟催化剂,DBU,二甲基哌嗪,催化剂TMR-2,-聚氨酯催化剂生产厂家 | 电脑知识|软件|系统|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网 | 智能风向风速仪,风速告警仪,数字温湿仪,综合气象仪(气象五要素)-上海风云气象仪器有限公司 | 无线讲解器-导游讲解器-自助讲解器-分区讲解系统 品牌生产厂家[鹰米讲解-合肥市徽马信息科技有限公司] | 纸箱抗压机,拉力机,脂肪测定仪,定氮仪-山东德瑞克仪器有限公司 | 铣刨料沥青破碎机-沥青再生料设备-RAP热再生混合料破碎筛分设备 -江苏锡宝重工 | 闭端端子|弹簧螺式接线头|防水接线头|插线式接线头|端子台|电源线扣+护线套|印刷电路板型端子台|金笔电子代理商-上海拓胜电气有限公司 | 海外仓系统|国际货代系统|退货换标系统|WMS仓储系统|海豚云 | 耐磨焊丝,堆焊焊丝,耐磨药芯焊丝,碳化钨焊丝-北京耐默公司 | 仓储货架_南京货架_钢制托盘_仓储笼_隔离网_环球零件盒_诺力液压车_货架-南京一品仓储设备制造公司 | 深圳善跑体育产业集团有限公司_塑胶跑道_人造草坪_运动木地板 | 船用泵,船用离心泵,船用喷射泵,泰州隆华船舶设备有限公司 | 深圳办公室装修,办公楼/写字楼装修设计,一级资质 - ADD写艺 | 山东螺杆空压机,烟台空压机,烟台开山空压机-烟台开山机电设备有限公司 | 拖链电缆_柔性电缆_伺服电缆_坦克链电缆-深圳市顺电工业电缆有限公司 | 四川成人高考_四川成考报名网 | 烟台条码打印机_烟台条码扫描器_烟台碳带_烟台数据采集终端_烟台斑马打印机-金鹏电子-金鹏电子 | 深圳装修_店面装修设计_餐厅设计_装修全包价格-尚泰装饰设计 | 贵州自考_贵州自学考试网| 轻型地埋电缆故障测试仪,频响法绕组变形测试仪,静荷式卧式拉力试验机-扬州苏电 |