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

      • <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 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

      时间:2023-09-22
          <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 id='B1yYx'><tfoot id='B1yYx'></tfoot><dl id='B1yYx'><fieldset id='B1yYx'></fieldset></dl></div>
        • <tfoot id='B1yYx'></tfoot>
              <tbody id='B1yYx'></tbody>

            <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 的 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模板网!

                上一篇:基于多个 ID 检索 Laravel 模型结果 下一篇:PHP foreach 循环键值

                相关文章

                最新文章

                  <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 id='pPWLM'><tfoot id='pPWLM'></tfoot><dl id='pPWLM'><fieldset id='pPWLM'></fieldset></dl></div>

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

                  1. <tfoot id='pPWLM'></tfoot>

                  2. <legend id='pPWLM'><style id='pPWLM'><dir id='pPWLM'><q id='pPWLM'></q></dir></style></legend>