That a Combobox memorizes the previous value


You see, I'm using combobox for the first time in a Laravel form.

I have this model:

    Schema::create('fotos', function(Blueprint $table){
        $table->unsignedInteger('categoria_id'); // Forarea de Categoria.
        $table->unsignedInteger('user_id'); // Foranea de User.

For what interests me we take the variable categoria_id , which is foreign from another table called Category.

I have this code snippet:

<div class="form-group row">
    <label for="categoria_id" class="col-md-4 col-form-label text-md-right">Categoría de la foto</label>

    <div class="col-sm-6">
         <select name="categoria_id" class="form-control">
            @foreach($categorias as $categoria) 
            <option value="{{ $categoria->id }}">{{$categoria->nombre}}</option> 

Which gives me this:

The combobox works, but let's imagine that I'm trying to modify an existing photo. I want the form to show what the current value of the photo is. How do I achieve that?

asked by Miguel Alparez 12.12.2018 в 14:05

1 answer


I managed to solve it myself. This is the code:

@foreach($categorias as $categoria) 
    <option value="{{ $categoria->id }}" {{$f->categoria_id==$categoria->id ? 'selected' : ''}}>{{$categoria->nombre}}</option> 

With $f the photo I want to modify.

answered by 13.12.2018 в 12:09