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

sakti's avatar
Level 1

How to save selected checkbox value to database in laravel?

i have report table and admin_report table. in admin_report table, the admin create the report options (checkbox value) to be displayed in report form. report table is basically is for if the user report the post it will be saved in report table. so now i want to save the multiple checkbox value in array form in database. how can i create store function for that? admin_report_id is foreign key report table.

i want to save multiple checkbox value to database. How to create store controller for this ?

blade:

            @foreach ($admin_report as $key => $report)
             <label class="container2">
                   <input type="checkbox" name="report[]" value="{{$report->id}}"> {{$report-
            >admin_report}}
              <span class="checkmark"></span>
                  </label>

           @endforeach 
0 likes
8 replies
rob897's avatar
public function store(Request $request)
{
    foreach ($request->report as $key => $value) {
        // do you insert here
    }
}
sakti's avatar
Level 1

what should include inside that? is my blade is correct?

sakti's avatar
Level 1

its showing Invalid argument supplied for foreach()..

biishmar's avatar
@foreach ($admin_report as $key => $report)
             <label class="container2">
                   <input type="checkbox" name="report[{{$report->id}}]"> {{$report-
            >admin_report}}
              <span class="checkmark"></span>
                  </label>

           @endforeach 

From Controller

public function store(Request $request)
{
    array_keys($request->report)
}

By this u get the checked report id, then use relation save or create method.. if its many to many then use relation sync
sakti's avatar
Level 1

showing this error now : array_keys() expects parameter 1 to be array, string given

controller:

          foreach ($report as $key => $report) {
            $data = $request->all();
           array_keys($request->report);
        $data = $request->all();
        $user = auth()->user();
        $user->report()->create([
            'report' => $data['report'],'postqs_id' => $id,'admin_report_id'=>$id
       
         ]);
biishmar's avatar

because you are using array_keys inside foreach use outside and dd it.

sakti's avatar
Level 1

again this error :array_keys() expects parameter 1 to be array, string given

girishgv21's avatar
for ($i = 0; $i < count($request->input('report')); $i++) {
            if ($i == 0) {
                $tableName->fieldName .= $request->input('report')[$i];
            } else {
                $tableName->fieldName .= ','.$request->input('report')[$i];
            }
}

Please or to participate in this conversation.