4
I need to display the name of the doctor who requested a patient report, however, with Laravel I tried to show this information on View thus:
{{laudo->paciente->medico->nome}}
Then I received the error message:
Trying to get Property of non-object
How do I query using the Query Builder according to the principle of this query:
select m.nome from medicos m, pacientes p, laudos l where m.id=p.medico_id and p.id=l.paciente_id and l.id=5;
Class Doctor
class Medico extends Model{
//
protected $fillable = [
"nome", "crm", "email", "datanascimento", "senha"
];
protected $table = 'medicos';
public function pacientes(){
return $this->hasMany('App\Paciente');
}
}
Class Patient
class Paciente extends Model{
//
protected $fillable = [
"medico_id", "nome", "rg", "email", "datanascimento", "senha"
];
protected $table = 'pacientes';
public function medico(){
return $this->belongsTo('App\Medico', 'medico_id');
}
public function laudos(){
return $this->hasMany('App\Laudo');
}
}
Class Report
class Laudo extends Model{
//
protected $fillable = [
"codigo", "paciente_id", "nome_arquivo", "data_emicao"
];
protected $table = 'laudos';
public function paciente(){
return $this->belongsTo('App\Paciente', 'id');
}
}
You are returning: "Errorexception: Trying to get Property of non-object"
– user67223
In the example I am loading a report report report. If you want to return only one report, you can filter by id. Report::find($id)->with('patient.medico'); ou Report::Where('id', $id)->with('patient.medico')->first>();
– Lucas Siqueira