That a Combobox memorizes the previous value

0

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->increments('id');
        $table->string('foto');
        $table->string('nombre')->unique();
        $table->date('fecha');
        $table->unsignedInteger('categoria_id'); // Forarea de Categoria.
        $table->foreign('categoria_id')->references('id')->on('categorias');
        $table->unsignedInteger('user_id'); // Foranea de User.
        $table->foreign('user_id')->references('id')->on('users');
        $table->timestamps();
    });

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> 
            @endforeach
        </select>
   </div>
</div>

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
source

1 answer

0

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> 
@endforeach

With $f the photo I want to modify.

    
answered by 13.12.2018 в 12:09