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

Using pivot model data as a relationship to another model in Laravel 4#39;s Eloquent(在 Laravel 4 的 Eloquent 中使用枢轴模型数据作为与另一个模型的关系)
本文介绍了在 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 中使用枢轴模型数据作为与另一个模型的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)