Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

peterpan26's avatar

laravel 8 checkbox , store working but deletes de last views when a new one is inserted

Hello, good morning, I come accross the following problem, i have some checkboxes, they store correctly and appear on the phpmy admin, but the prevous submited forms on the view page only show the last inserted, i have this code:

view blade file
<td>
                                    @foreach($item->motivo_deslocacao as $motivo)
                                        {{$motivo}},
                                    @endforeach
                                </td>
                                <td>
                                    @foreach($item->servico_afetacao as $servico)
                                        {{$servico}},
                                    @endforeach
                                </td>

create.blade file
  <input type="checkbox" class="form-check-input" name="motivo_deslocacao[]" value="motiv">motiv
<input type="checkbox" class="form-check-input" name="motivo_deslocacao[]" value="motivdois">motivdois
<input type="checkbox" class="form-check-input" name="servico_afetacao[]" value="serve">serve
<input type="checkbox" class="form-check-input" name="servico_afetacao[]" value="servedois">servedois

model
protected $casts = [
        'motivo_deslocacao' => 'array',
        'servico_afetacao' => 'array',
    ];

controller
public function create(Request $request)
    {
        $motivo_deslocacao = array($request->motivo_deslocacao);
        $servico_afetacao = array($request->servico_afetacao);
        return view('admin.formulario.create', [
            'viaturas' => Viaturas::all(),
            'motivo_deslocacao' => $motivo_deslocacao,
            'servico_afetacao' => $servico_afetacao,
        ]);
    }
public function store(Request $request)
    {
$data->motivo_deslocacao = $request->input('motivo_deslocacao');
        $data->servico_afetacao = $request->input('servico_afetacao');
 $data->save();
}

if someone know how to fix this pls tell me

0 likes
4 replies
peterpan26's avatar

@lovertohelp public function destroy($id) { $formulario = Formulario::find($id); $formulario->delete(); return redirect()->back()->with('status', 'Formulario Deleted Successfully'); }

Please or to participate in this conversation.