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

royo1987's avatar

Update empty checkboxes

Hi, I'm using Laravel 5.7 I have a list of more than 500 checkboxes that Im inserting into the database with the help of @kfirba s library. Every single checkbox represents a registry. Table captures structure

Schema::create('captures', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id');
            $table->integer('pokemon_id');
            $table->boolean('catched')->default(0);
            $table->boolean('male')->default(0);
            $table->boolean('female')->default(0);
            $table->boolean('hundo')->default(0);
            $table->boolean('cero')->default(0);
            $table->boolean('lucky')->default(0);
            $table->boolean('shiny')->default(0);
            $table->boolean('shadow')->default(0);
            $table->boolean('purified')->default(0);
            $table->timestamps();
            
            $table->foreign('pokemon_id')->references('id')->on('pokemon')->onUpdate('cascade')->onDelete('cascade');     
            $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');    
            $table->unique(['pokemon_id','user_id']);
            
        });

CaptureController.php

    public function storeList(Request $request, Pokemon $pokes)
    {
        //
        $catches = ($request->input('pokes'));

        $data = array();
        
        //'catched','male','female','hundo','cero','lucky','shiny','shadow','purified','created-at'
        $excludedColums = [          
            'male','female','hundo','cero','lucky','shiny','shadow','purified'
        ];
        
        foreach ($catches as $catch){
             $data [] = [
                 'user_id' => Auth::user()->id,
                 'pokemon_id' => intval($catch),
                 'catched' => '1',
             ];
        }
        
        $query = (new QueryGenerator)->generate('captures',$data,$excludedColums);
        
        DB::select($query->getQuery(),$query->getBindings());
        return redirect()->back();
    }

With that I can create or update at the same time all the records that where checked in the blade, my problem is: how can I get all the unchecked boxes that the user unchecked? so I can update it in the database to 0. Thanks for your time and help!

0 likes
10 replies
mstrauss's avatar

@royo1987

Maybe consider radio inputs instead of checkboxes. You may also be able to add a default value to each checkbox. But I'm not 100% sure on that.

1 like
royo1987's avatar

Hi @mstrauss I have never used radios, I'll look into them and go back to you, thanks!

Sinnbeck's avatar

Or you can use Javascript to iterate over your checkboxes and check their status. A simple html will not post anything if a checkbox is unchecked

If you have an array of items that are possible checkboxes you can also just remove each item from that array when it is checked. That should leave you with just the ones that are unchecked

royo1987's avatar

@mstrauss on a quick research you can not remove the radio selection, if you click on it, you need to click in another one to remove the first one. So radios wont help my case. Thanks

royo1987's avatar

@sinnbeck I've no idea how to use JavaScript, but I'll research about it, youtube tutorials here I go.

Sinnbeck's avatar

Like I said. If you know the exact possible checkboxes, you can do it in php

jlrdw's avatar
jlrdw
Best Answer
Level 75

You have to set up a loop, and using in_array, if the value is in the array then it is equal to 1, otherwise it is equal to 0 for storage in the database.

A similar Loop is needed if you pull up an edit page to determine if 0 or 1.

royo1987's avatar

Yes @jlrdw , that is what I ended up doing:

In my blade I added the list of id and previously checked values:

<input type="hidden" name="pokes[{{ $pokemon->id }}]" value={{$pokemon->catched}}>

in my CaptureController I did this:

    public function storeList(Request $request)
    {
        // $pokdes is an array of $pokemon->id => $pokemon->captured
        $pokes = $request->pokes;
        
        // $selections is a list ot the checkboxes that the user checked
        $selections = $request->input('selections');
        
        $data = array();
        
        // Go through the seleccions and add it to the data
        foreach ($selections as $selection) {
                $data [] =
                ['user_id' => Auth::user()->id,
                    'pokemon_id' => intval($selection),
                    'catched' => '1',];
        }
        
        // Go through the original pokes list
        foreach ($pokes as $pokemon => $capture){
            // if it was previously checked and its not in the selections list, then update to 0.
            if ($capture && !(in_array($pokemon, $selections))) {
                    $data [] =
                    ['user_id' => Auth::user()->id,
                        'pokemon_id' => intval($pokemon),
                        'catched' => '0',];
                }            
        }        
        
        //'catched','male','female','hundo','cero','lucky','shiny','shadow','purified','created-at'
        $excludedColums = [          
            'male','female','hundo','cero','lucky','shiny','shadow','purified'
        ];
       
        $query = (new QueryGenerator)->generate('captures',$data,$excludedColums);
        
        DB::select($query->getQuery(),$query->getBindings());
        return redirect()->back();
    }
Sinnbeck's avatar

If your question was solved by @jlrdw then please to remember to mark "best answer"

lemmon's avatar

If the checkbox is not checked, then it will not be included in the request, so is it there =1, not there =0

Please or to participate in this conversation.