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

peterpan26's avatar

laravel 8 checkbox inserting the wrong way probably?

hello i have a form in wich i have some checkboxes, i intend to store from the form in the form of array,( but it's saving in phpmyadmin with square brackets and " " )like this: ["optionselected1","optionselected2"] when i try to fetch those values in a dashboard in a different page they dont appear, i got this code:

//create.blade
 <div class="form-group mb-3">
                                            <label class="form-check-label">
                                                <input type="checkbox" class="form-check-input" name="opt[]" value="opt1">opt1
                                            </label>
                                        </div>
                                        <div class="form-group mb-3">
                                            <label class="form-check-label">
                                                <input type="checkbox" class="form-check-input" name="opt[]" value="opt2">opt2
                                            </label>
                                        </div>
<div class="form-group mb-3">
                                            <label class="form-check-label">
                                                <input type="checkbox" class="form-check-input" name="sf[]" value="opt1">opt1
                                            </label>
                                        </div>
                                        <div class="form-group mb-3">
                                            <label class="form-check-label">
                                                <input type="checkbox" class="form-check-input" name="sf[]" value="opt2">opt2
                                            </label>
                                        </div>
//controller
public function create(Request $request)
{
    $opt= array($request->opt);
    $sf = array($request->sf);
return view('admin.formulario.create', [
        'viaturas' => Viaturas::all(),
        'opt' => json_encode($opt),
        'sf' => json_encode($sf),
    ]);
}
//store function
$data->opt= $request->input('opt');
 $data->sf= $request->input('sf');
//and in fetch page
<tbody>
                            @foreach ($formulario as $item)
                            <tr>
                                <td>{{ $item->opt}}</td>
                                <td>{{ $item->sf}}</td>

how do i fetch correctly?? thanks in advance

0 likes
8 replies
alya_alsiyabi's avatar

that is means they are in Json format as array so just do something like this in your view :

<?php
$ss=json_decode($item->sf);
?>   
@foreach 
{{$ss}}
@Endforeach```
peterpan26's avatar

@mohamedtammam i've tried putting this you said, but its giving this error when i try to open the dashboard page: json_decode(): Argument #1 ($json) must be of type string, array given

peterpan26's avatar

it's giving, error, in model right? i got it like this class Formulario extends Model { use HasFactory; protected $table = 'formulario';

protected $casts = [
    'opt' => $opt,
    'sf' => $sf,
];

}

peterpan26's avatar

i've got this way, but its not working.

public function create(Request $request)
{
    $opt= array($request->opt);
    $sf= array($request->sf);
    return view('admin.formulario.create', [
        'viaturas' => Viaturas::all(),
        'opt' => json_encode($opt),
        'sf' => json_encode($sf),
    ]);
    
}
public function store(Request $request){
 $opt= json_decode($request->opt);
    $sf= json_decode($request->sf);
--------------------------------------------------
 <?php
                                $ss = json_decode($item->opt);
                                $dd = json_decode($item->sf);
                                ?>
                                @foreach
                                {{$ss}}
                                {{$dd}}
                                @Endforeach

it appears the error is undefined array key 1

peterpan26's avatar

i've tried now and it was working i dont know, i made backup restarted computer and its no longuer working even the backup version:

<td>
                                    @foreach($item->opt as $value)
                                        {{$value}},
                                    @endforeach
                                </td>
                                            

Please or to participate in this conversation.