久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <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="qwawimqqmiuu" 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="qwawimqqmiuu" 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 的系統,并且給定的 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 指的是 多對多關系.它們都是不同的關系類型,每種都需要不同的數據庫結構 - 因此它們采用不同的參數.

                  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.

                  關鍵區別在于,在一對多關系中,您只需要與相關模型對應的兩個數據庫表.這是因為對關系的引用存儲在擁有模型的表本身中.例如,您可能有一個 Country 模型和一個 City 模型.一個國家有許多城市.但是,每個城市僅存在于一個國家/地區.因此,您可以將該國家/地區存儲在 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).

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

                  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.

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

                  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模板網!

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

                  相關文檔推薦

                  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的訪問被拒絕)
                      <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="qwawimqqmiuu" 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>
                          • 主站蜘蛛池模板: 一区二区三区视频 | 久久精品国产久精国产 | 欧美国产日本一区 | 久久久av| 日韩精品久久久久久 | 久久久国产精品一区 | 亚欧洲精品在线视频免费观看 | 在线色网 | 玖玖视频 | 亚洲人精品午夜 | 久久久久国产 | 狠狠婷婷综合久久久久久妖精 | 日韩另类 | 天天拍天天色 | 在线观看免费av网 | 欧美亚洲视频在线观看 | 日韩有码在线观看 | 亚洲成人三级 | 三级视频在线观看电影 | 亚洲人成在线观看 | 综合另类 | 午夜成人免费视频 | 久草久| 亚洲伊人精品酒店 | 国产91丝袜在线播放 | 噜久寡妇噜噜久久寡妇 | 国产伦一区二区三区久久 | 亚洲欧美一区二区三区国产精品 | 小川阿佐美pgd-606在线 | 久久久久久国产精品免费 | 国产成人精品久久 | 国产高清一区二区 | 亚洲欧洲精品在线 | 一级毛片在线播放 | 美女视频一区 | 青青草社区| 久久久久国产精品一区二区 | 96久久久久久 | 给我免费的视频在线观看 | 在线观看中文字幕视频 | 黄色一级电影在线观看 |