How to do a search with Query Builder in Laravel?

4

I need to display the name of the doctor who requested a patient report, but with Laravel I tried to show this information in View like this:

{{laudo->paciente->medico->nome}}

Then I received the error message:

  

Trying to get property of non-object

How do I query using Query Builder at the beginning 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;
  

Medical Class

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 Award

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');
    }
}
    
asked by anonymous 01.11.2017 / 15:19

2 answers

0

To display the name of the doctor in the view it is necessary to access the data of the second way:

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

The data is stored in an array.

    
01.11.2017 / 18:12
4

When you load the reports, use Eager Loading , which will also load records from other related tables:

$laudos = Laudo::with('paciente.medico')->get();

Source: link

    
01.11.2017 / 17:03