Select no Laravel is returning the column name (Eloquent)

Asked

Viewed 127 times

1

I’m having trouble returning only the value of select in the case is the name ,but it is returning as if it were string and this with column name.

Controller

public function resultadoconsulta(Request $request){                    
        $nome = $request-> nomeCompleto;
        $data = $request-> dataNascimento;

 $paciente = Pacientes::select('nome_completo')                             
             ->where('nome_completo',$nome)                                 
             ->where('data_nascimento',$data)                               
             ->get();

 return view('/resultadoconsultapaciente')->withPaciente(json_encode($paciente));}

View

{{$paciente}}

Upshot

[{"nome_completo":"Leonardo Amancio de Araujo"}]
  • Why are you using json_encode?

  • Research could also be better

1 answer

1

Controller

public function resultadoconsulta(Request $request){                    
        $nome = $request-> nomeCompleto;
        $data = $request-> dataNascimento;

 $paciente = Pacientes::select('nome_completo')                             
             ->where('nome_completo',$nome)                                 
             ->where('data_nascimento',$data)                               
             ->get()->first(); 
//use o first() para obter o primeiro registo da lista, caso contrário devolve uma collection


//outra forma de passar a variável à View
 return view('/resultadoconsultapaciente', compact('paciente'))
}

View

{{$paciente->nome_completo}}
  • The result was almost the same : {"full name_name":"Leonardo Amancio de Araujo"}

  • Used ->first(); in query?

  • I used yes ,I could fix it like this: I put it in the view @foreach($patient as $Pac) and then {{$Pac->full name_name}}

Browser other questions tagged

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