問題描述
我有一個用于多對多關系的數據透視表,其中包括另一個模型的第三個索引參數.我希望能夠使用 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 中使用樞軸模型數據作為與另一個模型的關系的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!