How to store checkbox values if check box is in foreach loop (laravel)

Posted 1 year ago by irankhosravi

I have List of the infraction and All infraction is print in a foreach loop. I want to store some infraction id in the database using checkbox.

<table class="table table-bordered">
    @foreach($infractions as $infraction)
       <tr>
           <th>{{ $infraction->title }}</th>
           <td>
               <input type="checkbox" value="1" name="infraction{{ $infraction->id }}" data-toggle="toggle" data-on="Yes" data-off="No" data-onstyle="success" data-offstyle="danger">
               <input type="hidden" name="infraction_name{{ $infraction->id }}" value="{{ $infraction->id }}" />
           </td>
       </tr>
    @endforeach
</table>

I have no idea where I put submit button and how to get value from a checkbox into Controller to save data.

public function store(Request $request)
{
    for(int i=0;i<68;i++){
        $infraction_id=$_POST['infraction_name'.i];
        $infraction_true_false=$_POST['infraction'.i];
        $query="insert into inspection ('title') value ($infraction->id)";
    }
    auth()->user()->inspections()->create($request->all());
}

I want to change php (a example) to laravel.

Please sign in or create an account to participate in this conversation.