使用枢纽模型数据作为Laravel 4的雄辩中的另一个模型的关系 [英] 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.
在我的应用程序中,我有一个用户
谁可以拥有许多主题
和许多学期
。当用户具有主题
时,它也需要属于给定的学期
。我有一个用户
,主题
和学期
表,以及 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
).
当我检索到用户
我也希望能够获得会话
主题
已通过枢轴连接
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');
}
}
我想要做的是遵循,并且可以使用会话
模型。
What I would like to be able to do is as follows, and have the Session
model available to me.
$user->subjects()->pivot->semester;
这样的事情是可能的,还是需要扩展到口才?
Is such a thing possible, or will this require an extension to Eloquent?
推荐答案
有一种方法可以通过创建一个扩展 Illuminate\Database\Eloquent\Relations\枢
。虽然这种方法还需要为拥有这个数据枢纽的两个模型添加一些代码,以便在需要时使用新的数据透视图。
There is a way to do this by creating a class that extends 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.
此链接讨论了自定义数据透视图的实现型号:
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 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参考文献:使用数据透视表有一个关于定义自定义数据透视模型的简短说明。
Laravel Doc Reference: Working with Pivot Tables has a short passage about Defining a Custom Pivot Model.
这篇关于使用枢纽模型数据作为Laravel 4的雄辩中的另一个模型的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!