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

Fouadmar's avatar

How to save multiple checkboxes in Laravel

Please i need some help in my application. I am using Laravel 4.2 and MySQL database. I am trying to use Checkboxes as some information the user have to provide to the application by checking one or more checkboxes. View

This is my view code :

<label for="jeux_plan"><input id="jeux" name="pieceafournir[]" value="jeux" type="checkbox" ><div ><h4>{{trans('app.jeux')}}</h4></div></label>
<label for="photocopin"><input id="photocopie" type="checkbox" name="pieceafournir[]" value="photocopie" ><div ><h4>{{trans('app.photocopie')}}</h4> </div></label>
<label for="contrat"> <input id="contrat" type="checkbox" name="pieceafournir[]" value="contrat" ><div ><h4>{{trans('app.contrat')}}</h4></div></label>
<label for="certificat"><input id="certificat" type="checkbox" name="pieceafournir[]" value="certificat" ><div ><h4>{{trans('app.certificat')}}</h4></div></label>
<label for="plan"><input id="plan" type="checkbox" name="pieceafournir[]" value="plan" ><div ><h4>{{trans('app.plan')}}</h4></div></label>
<label for="cahier"><input id="cahier" type="checkbox" name="pieceafournir[]" value="cahier" ><div ><h4>{{trans('app.cahier')}}</h4></div></label>
<label for="demande"><input id="demande" type="checkbox" name="pieceafournir[]" value="demande_autorisation" ><div ><h4>{{trans('app.demande')}}</h4></div></label>
<label for="identification"><input id="identification" type="checkbox" value="identification" name="pieceafournir[]" ><div ><h4>{{trans('app.identification')}}</h4></div></label>

In my model , i have one to many relationship between Project and Pieces.

Project(#id,project_name,project_city,...,) 
Pieces(#id,#project_id,jeux,photocopie,contrat,certificat,plan,cahier,demande,identification)

As you have noticied i am using boolean attribute in my Pieces table, so if the checkbox is checked it's a 1, otherwise it's a zero. ( I dont know if it's the best way to do things or not, meanwhile i'm open to any proposition of another efficient solution).

Now in my controller, i am getting all the information by :

$data = \Input::all();
$project = new Project;
$pieces = new Pieces; 

$project->project_name = $data['project_name'];
... // Other instructions
... 
save(); 

I am wondering how can i use the checkboxes and save them. If i am getting the checkboxes value in an array. Am i going to use a two dimensional array to save them ? I'm really blocked over here please can anyone help me ! Thanks for reading .

0 likes
1 reply
martinbean's avatar

@Fouadmar You could give the columns are default value of 0 in your database table. Then, in your controller, only checked checkedboxes will pass their value, so you could use the values as keys, and set the values to 1 to update the corresponding columns. Something like:

$pieces = array_flip(Input::get('piecafournir'));

foreach ($pieces as $value => $key) {
    $pieces[$value] = true;
}

$pieces will not be an array where the keys are things like jeux, and all the values are true, so you can pass this to your model:

Pieces::fill($pieces);

Please or to participate in this conversation.