复杂多对多Laravel 4

时间:2014-11-27 17:13:47

标签: php laravel-4 eloquent

我必须做以下关系: enter image description here

任何想法如何与Eloquent laravel实现这种关系?

我在执行"药物","处方"之间的关系时遇到了麻烦。和"小时"

1 个答案:

答案 0 :(得分:1)

对于关系" prescription_drug_has_hour"你有n-ary的关系,正好是3个表,你可以像普通的ManyToMany那样做:

relations on Prescription:
belongsToMany('Drug','prescription_drug_has_hour')->withPivot('hour_id');
belongsToMany('Hour','prescription_drug_has_hour')->withPivot('drug_id');
// and so on

对于"药物"有积极的原则它只是一个普通的ManyToMany(没有withPivot

请注意,如果您希望在拥有属性的关系时提供更多属性,则可以将它们添加到withPivot函数中,如下所示:

// prescription_has_drug
// ...
// relation on Prescription:
belongsToMany('Drug','prescription_has_drug')->withPivot('via_id', 'method_id', 'miligram', 'observation');