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

mozew's avatar
Level 6

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

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.

0 likes
14 replies
mozew's avatar
Level 6

I searched but did not find anything.

Cronix's avatar

I'm not going to go through it all (again)...so hopefully you can figure it out with a basic example. Notice they use the same name using [ ] which is HTML array syntax

@foreach($infractions as $infraction)
    <input type="checkbox" name="infraction[]" value="{{ $infraction->id }}">
@endforeach 

In controller...

// this is an array, because we used the same name with []
$infractions = $request->infraction; 

foreach ($infractions as $infraction_id) {
    //$infraction_id is the infraction_id that was checked
}

NOTE: only checked boxes will be present in the request. Unchecked boxes don't get sent in the request, as per HTML specs.

2 likes
Cronix's avatar

As I said, I'm not going to do it all. You should be able to tell that from my code in the controller where I'm looping over $infractions to get the ids... That part is basic laravel.

I couldn't tell you how to save anyway, as you don't show any relationships or anything, or what it is you're trying to save. What data do you need to create a new inspection?

jlrdw's avatar

Do a normal form, not foreach a form. Look at some of those examples. Remember a checkbox has no POST if not checked,

You have to check that in controller.

That's why I showed the:

if($request->has ...

Cronix's avatar

@jlrdw The foreach to create the checkboxes is fine... It's coming from the database, so you should foreach over it to create the checkboxes...

vu7675's avatar
<table class="table table-bordered">
    @foreach($infractions as $infraction)
       <tr>
           <th>{{ $infraction->title }}</th>
           <td>
               <input type="checkbox" name="infraction_id[{{ $infraction->id }}]" value="{{ $infraction->id }}" />
           </td>
       </tr>
    @endforeach
</table>

public function store(Request $request)
{
    if($request['infraction_id']!= null){
            foreach ($request['infraction_id'] as $inf){
               $query="insert into inspection () value ()";
            }
        }
        auth()->user()->inspections()->create($request->all());
}

mozew's avatar
Level 6

My field is title in database and my checkbox name is infraction_id[{{ $infraction->id }}].

jlrdw's avatar

I have worked with checkboxes for years, and it seems so neat to do an array, but the array becomes a mess to deal with. I like working with individual checkboxes. Just a little more code.

Any example convert to laravel request methods.

In controller:

$adopted = (isset($_POST['adopted']) == '1' ? '1' : '0');

If adopted was not passed

The animal's checkbox wasn't checked thus a

0 is stored

And the animal is still available for adoption.

Otherwise a 1 (one) is stored and the animal has been adopted.

In a view when showing form

<input type="checkbox" name="adopted" id="adopted" value="1"<?php echo ($cat->adopted == 1 ? ' checked' : ''); ?>>

So if a 1 (one) is already stored in the database, the checkbox will be checked. Otherwise the checkbox will not be checked.

If you stay away from that multi check array stuff you won't have these problems.

Just work with individual single checkboxes. So there's 20 of them, so what.

mozew's avatar
Level 6

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

Please or to participate in this conversation.