How do I search with Query Builder in Laravel?

Asked

Viewed 397 times

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');
    }
}

2 answers

4

  • You are returning: "Errorexception: Trying to get Property of non-object"

  • 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>();

0


To display the doctor’s name in the view you will need to access the data in the following way:

{{$laudo->paciente["medico"]["nome"]}}

Data is stored in an array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.