Laravel - Bring SELECT Component ALREADY SELECTED when editing

Asked

Viewed 95 times

-2

I am trying to bring a select already selected according to the information that is recorded in the bank. However I am having a lot of difficulty. I’ll leave the code below.

Controler

public function ListaUf()
{
    $ListaUf = Estado::all();
    return view(‘psicologo’,compact(‘ListaUf’));
}

Routes

Route::get(’/psicologo/editar/{psi_codigo}’,‘ControladorMunicipioUf@ListaUf’);

View

<select>
    @foreach($ListaUf as $uf)  
        <option {{$psi->uf_codigo=={{$uf->uf_codigo}} ? 'selected':''}} value="{{$uf->uf_codigo}}">{{$uf->uf_estado}}</option>
    @endforeach
</select>
  • Are you making a mistake? Where specifically is the difficulty?

  • I modified the code. it’s like this now.

  • Try to change {{$psi->uf_codigo=={{$uf->uf_codigo}} ? 'selected':''}} for {{$psi->uf_codigo == $uf->uf_codigo ? 'selected' : ''}}

1 answer

0

If you are looking for a specific state, use:

public function ListaUfEditar($id)
{   
   $ListaUf = Estado::find($id);
   return view('psicologo',compact('ListaUf'));

}

Or you can pass an array of ids to then return a Collection.

To search all states in a Collection to iterate in your view use the "all()" method without arguments.

https://laravel.com/docs/5.7/eloquent

Browser other questions tagged

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