是否可以在Laravel中获得数据透视表与另一个表的关系? [英] Is it possible to get the relation of a pivot table to another table in Laravel?
问题描述
我有一个培训应用程序,当前具有以下架构:
I have a training app that currently has this schema:
trainings
id
name
speakers
id
first_name
last_name
training_speaker
id
training_id
speaker_id
training_speaker_dates
id
training_speaker_id
date
time
一个培训可以有多个演讲者,一个演讲者可以有多个培训.目前,我正在通过其枢纽模型来访问培训演讲者.
A training can have multiple speakers, and a speaker can have multiple trainings. Currently, I'm accessing the training speakers by its pivot model.
class Training {
public function speakers() {
return $this->belongsToMany('App\Speaker')->using('App\TrainingSpeaker');
}
}
假设我们接受了为期3天的培训,发言人可以参加第一天甚至是早上的演讲,这就是training_speaker_dates
具有日期和时间(下午)的原因.
Let's say we have a 3-day training, a speaker can attend the first day and maybe just the morning, that's why the training_speaker_dates
has date and time(morning of afternoon).
是否可以在Training类的Speakers方法中也包含training_speaker_dates
?
Is it possible to also include the training_speaker_dates
in the speakers method under Training class?
或者,我应该只删除training_speaker_dates
并将字段添加到training_speakers
表中,并允许具有唯一ID的重复值(training_id和Speaker_id)吗?所以看起来像这样
Or, should I just remove the training_speaker_dates
and add the fields to training_speakers
table and allow duplicate values (training_id & speaker_id) with unique id? So then it'll look like this
training_speaker
id
training_id
speaker_id
date
time
推荐答案
尝试一下
class Training {
public function trainingspeakerdates() {
return $this->hasMany('App\Trainingspeakerdates','training_speaker_id','id')->using('App\TrainingSpeaker');
}
}
这篇关于是否可以在Laravel中获得数据透视表与另一个表的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!