Using pivot model data as a relationship to another model in Laravel 4#39;s 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
.我有一个 users
、subjects
和 semesters
表,以及一个 subject_user
表(其中有 user_id
、subject_id
和 semester_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 中使用枢轴模型数据作为与另一个模型的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Laravel 4 的 Eloquent 中使用枢轴模型数据作为与另一个模型的关系
基础教程推荐
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01