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.
Update empty checkboxes
Hi, I'm using Laravel 5.7 I have a list of more than 500 checkboxes that I’m 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!
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.
Please or to participate in this conversation.