each patient can have multiple files. ( one for each hospital admission )
Each patient can have multiple doctors through files ( at each hospital admission, patient can have a different doctor )
Each file can have just one doctor and just one patient ( each file is created at hospital admission.
Each doctor can have multiple patients and multiple files ( all his patients files )
how can i write the relations between these three models?
The tricky part is that a patient can have multiple files but a file a single patient so how can i write the reverse of the relation? Patient hasMany file, but the reverse will be something like file hasOne patient. Is that possible?
//-------------- Patient Model Class----------
class Patient extends Model {
public function files()
{
return $this->hasMany('App\File');
}
}
//-------------- File Model Class----------
class File extends Model {
public function patient()
{
return $this->belongsTo('App\Patient');
}
}
@kmunky This isn't tested but should get you going.
Patient model
public function files() {
return $this->hasMany('App\File');
}
public function doctors() {
return $this->hasManyThrough('App\Doctor', 'App\Patient');
}
Doctor model
public function patients() {
return $this->hasMany('App\File');
}
public function files() {
return $this->hasManyThrough('App\Doctor', 'App\Patient');
}
File model
public function doctor() {
return $this->hasOne('App\Doctor');
}
public function patient() {
return $this->belongsTo('App\Patient');
}