Retrieve data from several checkboxes when editing a Laravel form

0

I have an edit form that has many checkbox fields and I need to recover them in the edit view,

view:

<div class="uk-container">

<div class="uk-margin uk-text-left" uk-grid>
    <legend class="uk-legend"><strong>4. Requerimientos de Calidad</strong></legend>
        <div class="uk-width-1-3"> 
        {!! Form::label('quality_control', '¿Realizar control de calidad?',['class' => '']) !!}
        {!! Form::select('quality_control',['' => 'Seleccione','Si' =>'Si','No' =>'No'],false,['class' => 'uk-select', 'required'])!!}
    </div>
</div>

<div class="uk-flex" uk-grid>

    <div class="uk-text-left uk-width-1-3">
        <label>4.1. Pruebas sobre Propiedades Mecánicas</label><br>
        {!! Form::checkbox('protection[]', 'Resistencia a la Ruptura',false,['class'=>'uk-checkbox check-disabled']) !!} Resistencia a la Ruptura<br/>
        {!! Form::checkbox('protection[]', 'Resistencia al Impacto (Impacto al Dardo)',false,['class'=>'uk-checkbox check-disabled']) !!} Resistencia al Impacto (Impacto al Dardo)<br/>
        {!! Form::checkbox('protection[]', 'Coeficiente de Fricción',false,['class'=>'uk-checkbox check-disabled']) !!} Coeficiente de Fricción<br/>
        {!! Form::checkbox('protection[]', 'Coeficiente de Barrera',false,['class'=>'uk-checkbox check-disabled']) !!} Coeficiente de Barrera<br/>
        {!! Form::checkbox('protection[]', 'Prueba de Tracción',false,['class'=>'uk-checkbox check-disabled']) !!} Prueba de Tracción<br/>
        {!! Form::checkbox('protection[]', 'Prueba de Elongación',false,['class'=>'uk-checkbox check-disabled']) !!} Prueba de Elongación
    </div>

    <div class="uk-text-left uk-width-1-3">
        <label>4.2. Pruebas sobre Propiedades de Barrera</label><br>
        {!! Form::checkbox('protection[]', 'Permeabilidad al Vapor de Agua',false,['class'=>'uk-checkbox check-disabled']) !!} Permeabilidad al Vapor de Agua<br/>
        {!! Form::checkbox('protection[]', 'Permeabilidad al Oxigeno',false,['class'=>'uk-checkbox check-disabled']) !!} Permeabilidad al Oxigeno<br/>
        {!! Form::checkbox('protection[]', 'Permeabilidad a la transmisión de Luz',false,['class'=>'uk-checkbox check-disabled']) !!} Permeabilidad a la transmisión de Luz
    </div>

    <div class="uk-text-left uk-width-1-3">
        <label>4.3. Pruebas Microbiológicas</label><br/>
        {!! Form::checkbox('protection[]', 'Mesófilos',false,['class'=>'uk-checkbox check-disabled']) !!}  Mesófilos<br/>
        {!! Form::checkbox('protection[]', 'Coliferos',false,['class'=>'uk-checkbox check-disabled']) !!} Coliferos<br/>
        {!! Form::checkbox('protection[]', 'Mohos y Levaduras',false,['class'=>'uk-checkbox check-disabled']) !!} Mohos y Levaduras<br/>
        {!! Form::checkbox('protection[]', 'E-coli',false,['class'=>'uk-checkbox check-disabled']) !!} E-coli
    </div>

     <div class="uk-text-left uk-width-1-3">
        <label>4.3. Pruebas de Migración</label><br/>
        {!! Form::checkbox('protection[]', 'Global',false,['class'=>'uk-checkbox check-disabled']) !!}  Global<br/>
        {!! Form::checkbox('protection[]', 'Especifica',false,['class'=>'uk-checkbox check-disabled']) !!} Especifica<br/>
        {!! Form::checkbox('protection[]', 'Metales Pesados',false,['class'=>'uk-checkbox check-disabled']) !!} Metales Pesados
    </div>

    <div class="uk-margin uk-text-left uk-width-1-3">
        <label>4.4. Certificaciones</label><br/>
        {!! Form::checkbox('protection[]', 'BPM',false,['class'=>'uk-checkbox check-disabled']) !!}  BPM<br/>
        {!! Form::checkbox('protection[]', 'BASC',false,['class'=>'uk-checkbox check-disabled']) !!} BASC<br/>
        {!! Form::checkbox('protection[]', 'NTC',false,['class'=>'uk-checkbox check-disabled']) !!} NTC<br/>
        {!! Form::checkbox('protection[]', 'ISO',false,['class'=>'uk-checkbox check-disabled']) !!} ISO
    </div>

    <div class="uk-margin uk-text-left uk-width-1-3">
        <label>4.5. Niveles de Calidad Aceptables</label><br/>
        {!! Form::checkbox('protection[]', 'ACS/AQL',false,['class'=>'uk-checkbox check-disabled']) !!} ACS / AQL
    </div>

</div>

driver:

public function edit($id){

    $datasheet = Datasheet::findOrFail($id);
    $structure = Structure::pluck('dsc_name_structure','id')->prepend('Seleccione', '')->toArray();
    return view('backend.profile.users.datasheet.edit')
    ->with('datasheet', $datasheet)
    ->with('structure', $structure);
}

Controller: store function

public function store(Request $request){

    $datasheets = new Datasheet($request->all());       
    $datasheets->user_id = auth()->id();
    $datasheets->protection = $request->protection;
    $datasheets->protection = implode(',', $datasheets->protection);
    $datasheets->save();
    session()->flash('success', 'Ficha creada correctamente');
    return redirect()->route('datasheet.index'); 
}

The checkboxes that I selected when creating the form separates them by comma (,) when creating the record in the BD, how could I retrieve that data and mark them in the respective position where I select them?

Thanks !!.

    
asked by Darwin Gomez 19.12.2017 в 04:35
source

1 answer

0

I understand that you are using Laravel Collective . In that case, you simply have to have an array with the values of the checboxes in the property protection (which is what the checkboxes are called).

Option 1:
Since you have them separated by commas, you should simply do an explode.

public function edit ($id) {
    $datasheet = Datasheet::findOrFail($id);
    $datasheet->protection = explode(',', $datasheet->protection);
    $structure = Structure::pluck('dsc_name_structure','id')->prepend('Seleccione', '')->toArray();
    return view('backend.profile.users.datasheet.edit')
    ->with('datasheet', $datasheet)
    ->with('structure', $structure);
}

Option 2:
Use a Accessor , but depending on how you use it, maybe this will bring you problems in others sides.

Datasheet.php:

public function getProtectionAttribute($valor)
{
    return explode(',', $valor);
}

Option 3:
Instead of saving separated by commas, save as JSON and use Attribute Casting , where you define that protection is a array . In this way, when saving, it automatically becomes JSON and when the value is obtained, it automatically becomes a array .

Datasheet.php:

protected $casts = [
    'protection' => 'array',
];

Controller:

public function store(Request $request){
    $datasheets = new Datasheet($request->all());       
    $datasheets->user_id = auth()->id();
    $datasheets->protection = $request->protection;
    $datasheets->save();
    session()->flash('success', 'Ficha creada correctamente');

    return redirect()->route('datasheet.index'); 
}
    
answered by 19.12.2017 в 07:43