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

2021-12-26 00:00:00 php laravel 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.

相关文章