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

Published 4 weeks 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.

jlrdw
jlrdw
4 weeks ago (237,650 XP)

If you could perhaps perform a search, a while back Cronix gave a splendid example of just that.

I was curious, so I did a quick search, so many previous good answers: https://laracasts.com/discuss?q=checkbox+array

See https://laracasts.com/discuss/channels/laravel/how-to-pass-checkbox-value-0-if-not-checked-and-1-if-checked-using-array if($request->has

irankhosravi

I searched but did not find anything.

Cronix
Cronix
4 weeks ago (647,500 XP)

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.

irankhosravi

@Cronix So how to save?

Cronix
Cronix
4 weeks ago (647,500 XP)

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
jlrdw
4 weeks ago (237,650 XP)

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
Cronix
4 weeks ago (647,500 XP)

@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...

webtrickshome

You should use implode() function to store all those values from checkboxes with same field name while storing in the database and use explode() function to break it down again before displaying it in the view files. Here's an example on storing multiple values from checkbox in basic php from webtrickshome from where you can get the basic ideas on how to handle it.

vincentnguyen
<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());
}

irankhosravi

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

jlrdw
jlrdw
4 weeks ago (237,650 XP)

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.

irankhosravi

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

jlrdw
jlrdw
4 weeks ago (237,650 XP)

Just convert to blade, I don't use blade myself. a lot of people use blade because of its HTMLentities built-in, me I just strip tags.

Another technique is the hidden technique see this post

https://laracasts.com/discuss/channels/laravel/blade-check-box-value

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