我知道这个话题有点混乱。让我解释一下。
对于我目前正在开发的医疗软件,我有如下数据结构:
患者s有协议s,协议s有检查s,处方s或E 110报告E 211s,均为多态。
在某些情况下,我必须直接访问Examinations of s,但是,由于这包括了检查和Protocol之间的多态关系,所以我无法在患者模型中直接建立关系方法。
作为一项工作,我可以设置一个自定义的getExaminationsAttribute,并在病人模型中添加$appends。但是,当我试图获取,例如,只有name的病人时,这会导致大量的数据获取。
任何帮助都是非常感谢的。
病人模型:
class Patient extends Model{
protected $table = 'patients';
public function protocols(){
return $this->hasMany('App\Protocol', 'patients_id', 'id');
}
protected $appends = ['examinations'];
public function getExaminationsAttribute()
{
$examinations = array();
$this->protocols->each(function($protocol) use (&$examinations){
$protocol->examinations->each(function($examination) use (&$examinations){
$examinations[] = $examination->toArray();
});
});
return $examinations;
}协议模型:
class Protocol extends Model{
protected $table = 'protocols';
public function patient(){
return $this->belongsTo('App\Patient', 'patients_id', 'id');
}
public function examinations()
{
return $this->morphedByMany('App\Examination', 'protocolable');
}考试模型:
class Examination extends Model{
protected $table = 'examinations';
public function protocols()
{
return $this->morphToMany('App\Protocol', 'protocolable');
}发布于 2017-10-30 11:50:28
作为一种解决方案,patients_id列添加到协议表的子模型中,如协议表、公式、等)。并将通过hasMany关系获取。
https://stackoverflow.com/questions/46827934
复制相似问题