久久久久久久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的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我很好奇為什么 hasMany 的 Eloquent 關(guān)系與 belongsToMany 的簽名不同.特別是自定義連接表名稱——對于給定 Comment 屬于許多 Role 的系統(tǒng),并且給定的 Role 將有許多 Comments,我想將關(guān)系存儲在一個名為 my_custom_join_table 的表中,并將鍵設(shè)置為 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 對象,但我使用非標(biāo)準(zhǔn)表名來存儲該關(guān)系,為什么可以我以一種方式使用這個非標(biāo)準(zhǔn)表而不是另一種方式?

                  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 用于 一對多關(guān)系,而 belongsToMany 指的是 多對多關(guān)系.它們都是不同的關(guān)系類型,每種都需要不同的數(shù)據(jù)庫結(jié)構(gòu) - 因此它們采用不同的參數(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.

                  關(guān)鍵區(qū)別在于,在一對多關(guān)系中,您只需要與相關(guān)模型對應(yīng)的兩個數(shù)據(jù)庫表.這是因為對關(guān)系的引用存儲在擁有模型的表本身中.例如,您可能有一個 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).

                  但是,多對多關(guān)系需要第三個??數(shù)據(jù)庫表,稱為數(shù)據(jù)透視表.數(shù)據(jù)透視表存儲對兩個模型的引用,您可以將其聲明為關(guān)系聲明中的第二個參數(shù).例如,假設(shè)您有 City 模型和 Car 模型.你想要一個關(guān)系來顯示人們在每個城市駕駛的汽車類型.好吧,在一個城市里,人們會駕駛許多 種不同類型的汽車.但是,如果您查看一種汽車類型,您也會知道它可以在許多不同的城市中行駛.因此,不可能在任一模型上存儲 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 關(guān)系,它只能與另一個 belongsToMany 關(guān)系配對,這意味著您有第三個數(shù)據(jù)透視表.如果您使用 hasMany 關(guān)系,它可以belongsTo 關(guān)系配對,并且不需要額外的數(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.

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

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

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

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

                  相關(guān)文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準(zhǔn)備好的語句amp;foreach 循環(huán))
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務(wù)器還是從同一用戶獲取記錄?)
                  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 查詢:用查詢結(jié)果填充變量)
                  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="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观看 | 日韩第一夜 | 日本人做爰大片免费观看一老师 | 成人在线视频一区二区三区 | 国产高潮好爽受不了了夜色 | 亚洲第一视频网 | 蜜桃视频在线观看免费视频网站www | 亚洲天堂中文字幕 | 成人网av| 999久久久 | 欧美日韩高清免费 | 久久久精品一区 | 欧美一区二区在线播放 | 日本精品视频在线观看 | 日韩视频在线免费观看 | 麻豆一区二区三区 | 久久久久亚洲精品国产 | 亚洲精品电影网在线观看 | 国产日韩欧美综合 | 精品视频免费 | 欧美5区 | 国产欧美精品区一区二区三区 | 欧美成人免费 | 中文字幕的av | 91精品国产综合久久婷婷香蕉 | 亚洲高清一区二区三区 | 精品久久国产 | 国产精品久久久久久久久久免费看 | 国产激情免费视频 | 国产婷婷精品av在线 | 国产一区二区三区四区五区3d | 日本中文在线视频 | 日本一区二区三区四区 | 欧美成年人 | 黄色网址在线播放 | 国产精品乱码一区二三区小蝌蚪 | 国产精品综合色区在线观看 |