That a checkbox retains an old value

0

You see, I have a table with these values:

Schema::create('contenidos', function (Blueprint $table) {
        $table->increments('id');
        $table->string('nombre');
        $table->timestamps();
});

I group all the rows in a vector called $ contents and use them for a form in the following way:

<div>
    <b><i><u>Habilidades requeridas:</u></i></b><br><br>
    @foreach($contenidos as $contenido)
        <input type="checkbox" name="capacidad[]" value="{{$contenido->id}}" {{ (is_array(old('capacidad')) and in_array(1, old('capacidad'))) ? ' checked' : '' }}> {{$contenido->nombre}}<br>
    @endforeach
</div>

What I want is that if an error occurs introducing the data of the form it is kept that icons were marked, but I find that every time that the form is restarted due to data not validated unlike the text boxes the checkboxes they are again unmarked. How do I solve this?

    
asked by Miguel Alparez 12.07.2018 в 16:55
source

1 answer

1

You can try the following, it should work:

<div>
    <b><i><u>Habilidades requeridas:</u></i></b><br><br>
    @foreach($contenidos as $contenido)
        <input type="checkbox" name="capacidad[]" value="{{$contenido->id}}" 
        {{ !empty(old('capacidad')) && in_array($contenido->id, old('capacidad')) ? 'checked' : '' }}> {{$contenido->nombre}}<br>
    @endforeach
</div>
    
answered by 12.07.2018 / 17:35
source