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

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

          <bdo id='B2dbB'></bdo><ul id='B2dbB'></ul>
      1. <i id='B2dbB'><tr id='B2dbB'><dt id='B2dbB'><q id='B2dbB'><span id='B2dbB'><b id='B2dbB'><form id='B2dbB'><ins id='B2dbB'></ins><ul id='B2dbB'></ul><sub id='B2dbB'></sub></form><legend id='B2dbB'></legend><bdo id='B2dbB'><pre id='B2dbB'><center id='B2dbB'></center></pre></bdo></b><th id='B2dbB'></th></span></q></dt></tr></i><div id='B2dbB'><tfoot id='B2dbB'></tfoot><dl id='B2dbB'><fieldset id='B2dbB'></fieldset></dl></div>
        <legend id='B2dbB'><style id='B2dbB'><dir id='B2dbB'><q id='B2dbB'></q></dir></style></legend>

        在 Laravel 4 的 Eloquent 中使用枢轴模型数据作为与

        时间:2023-09-22

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

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

                <tbody id='Wtwzv'></tbody>
                <bdo id='Wtwzv'></bdo><ul id='Wtwzv'></ul>
                • 本文介绍了在 Laravel 4 的 Eloquent 中使用枢轴模型数据作为与另一个模型的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个用于多对多关系的数据透视表,其中包括另一个模型的第三个索引参数.我希望能够使用 Eloquent 访问此模型.

                  I have a pivot table for a many to many relationship which includes a third index parameter for another model. I would like to be able to use Eloquent to access this model.

                  在我的应用程序中,我有一个 User,他可以拥有许多 Subjects 和许多 Semesters.当用户拥有 Subject 时,它也需要属于给定的 Semester.我有一个 userssubjectssemesters 表,以及一个 subject_user 表(其中有 user_idsubject_idsemester_id).

                  In my application, I have a User who can have many Subjects and many Semesters. When a user has a Subject, it needs to belong to a given Semester as well. I have a users, subjects and semesters table, as well as a subject_user table (which has user_id, subject_id and semester_id).

                  当我检索 User 的主题时,我还希望能够获得 Subject 已连接到的 Session通过数据透视表.

                  When I retrieve the User's subjects, I would also like to be able to get the Session the Subject has been connected to through the pivot table.

                  class User
                  {
                      public function subjects()
                      {
                          $this->belongsToMany('Subject')->withPivot('session_id');
                      }
                  }
                  

                  我希望能够做的如下,并让我可以使用 Session 模型.

                  What I would like to be able to do is as follows, and have the Session model available to me.

                  $user->subjects()->pivot->semester;
                  

                  这样的事情是可能的,还是需要对 Eloquent 进行扩展?

                  Is such a thing possible, or will this require an extension to Eloquent?

                  推荐答案

                  有一种方法可以通过创建一个扩展 IlluminateDatabaseEloquentRelationsPivot 的类来实现.尽管这种方法还需要向拥有此数据透视表的两个模型添加一些代码,以便它们在需要时使用新的数据透视表.

                  There is a way to do this by creating a class that extends IlluminateDatabaseEloquentRelationsPivot. Though this approach also requires adding some code to the two models that own this pivot so that they use the new Pivot when needed.

                  此链接讨论自定义枢轴模型的实现:https://github.com/laravel/framework/issues/2093#issuecomment-39154456

                  This link discusses the implementation of the Custom Pivot Model: https://github.com/laravel/framework/issues/2093#issuecomment-39154456

                  use IlluminateDatabaseEloquentRelationsPivot;
                  
                  class SubjectUser extends Pivot {
                     // add your relationships back to User and Subject
                      public function session() {
                           // your relation to session here
                      }
                  }
                  
                  class Subject extends Eloquent {
                      ...
                      public function newPivot(Eloquent $parent, array $attributes, $table, $exists) {
                          if ($parent instanceof User) {
                              return new SubjectUser($parent, $attributes, $table, $exists);
                          }
                          return parent::newPivot($parent, $attributes, $table, $exists);
                      }
                  }
                  
                  class User extends Eloquent {
                      ...
                      public function newPivot(Eloquent $parent, array $attributes, $table, $exists) {
                          if ($parent instanceof Subject) {
                              return new SubjectUser($parent, $attributes, $table, $exists);
                          }
                          return parent::newPivot($parent, $attributes, $table, $exists);
                      }
                  }
                  

                  现在您将可以访问已定义的枢轴关系.

                  Now you will have access to that pivot's relationship that have been defined.

                  $user->subjects->first()->pivot->session->...
                  

                  注意:您不会直接与该课程互动.当这两个模型之间需要枢轴时,会创建它而不是默认的枢轴.

                  Note: You will not be interacting with this class directly. It is created instead of the default Pivot when the pivot is needed between those 2 models.

                  Laravel 文档参考:使用数据透视表 有一小段关于定义自定义枢轴模型.

                  Laravel Doc Reference: Working with Pivot Tables has a short passage about Defining a Custom Pivot Model.

                  这篇关于在 Laravel 4 的 Eloquent 中使用枢轴模型数据作为与另一个模型的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:laravel eloquent 模型缓存 下一篇:Laravel bigInteger 在关系中四舍五入为 int

                  相关文章

                  最新文章

                • <i id='jsCOZ'><tr id='jsCOZ'><dt id='jsCOZ'><q id='jsCOZ'><span id='jsCOZ'><b id='jsCOZ'><form id='jsCOZ'><ins id='jsCOZ'></ins><ul id='jsCOZ'></ul><sub id='jsCOZ'></sub></form><legend id='jsCOZ'></legend><bdo id='jsCOZ'><pre id='jsCOZ'><center id='jsCOZ'></center></pre></bdo></b><th id='jsCOZ'></th></span></q></dt></tr></i><div id='jsCOZ'><tfoot id='jsCOZ'></tfoot><dl id='jsCOZ'><fieldset id='jsCOZ'></fieldset></dl></div>
                    <bdo id='jsCOZ'></bdo><ul id='jsCOZ'></ul>
                • <small id='jsCOZ'></small><noframes id='jsCOZ'>

                    <legend id='jsCOZ'><style id='jsCOZ'><dir id='jsCOZ'><q id='jsCOZ'></q></dir></style></legend><tfoot id='jsCOZ'></tfoot>