简体   繁体   English

使用枢轴模型数据作为与Laravel 4的Eloquent中另一个模型的关系

[英]Using pivot model data as a relationship to another model in Laravel 4's 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. 我希望能够使用Eloquent来访问此模型。

In my application, I have a User who can have many Subjects and many Semesters . 在我的应用程序,我有一个User谁可以有很多Subjects ,很多Semesters When a user has a Subject , it needs to belong to a given Semester as well. 当用户具有Subject ,它也必须属于给定的Semester I have a users , subjects and semesters table, as well as a subject_user table (which has user_id , subject_id and semester_id ). 我有一个userssubjectssemesters表,以及一个subject_user表(具有user_idsubject_idsemester_id )。

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. 检索User的主题时,我还希望能够通过数据透视表获取Subject已连接到的Session

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

What I would like to be able to do is as follows, and have the Session model available to me. 我想做的如下,并为我提供了Session模型。

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

Is such a thing possible, or will this require an extension to Eloquent? 这样的事情是否可能,或者这需要扩展到Eloquent?

There is a way to do this by creating a class that extends Illuminate\\Database\\Eloquent\\Relations\\Pivot . 有一种方法可以通过创建扩展Illuminate\\Database\\Eloquent\\Relations\\Pivot 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. 尽管此方法还需要向拥有此数据透视表的两个模型添加一些代码,以便它们在需要时使用新的数据透视表。

This link discusses the implementation of the Custom Pivot Model: https://github.com/laravel/framework/issues/2093#issuecomment-39154456 该链接讨论了自定义数据透视模型的实现: https : //github.com/laravel/framework/issues/2093#issuecomment-39154456

use Illuminate\Database\Eloquent\Relations\Pivot;

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 Doc Reference: Working with Pivot Tables has a short passage about Defining a Custom Pivot Model. Laravel Doc参考: 使用数据透视表有一小段关于定义自定义数据透视模型的文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM