show function and form delete
Nov 2, 2022
4
Level 1
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
Please or to participate in this conversation.