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

Nick385's avatar

How to handle checkboxes?

I feel kinda stupid for this question but i don't know how i do this :S

1st: How do i handle/store a checkbox properly. What do i put in my Model/controller/migration

2nd How do i fetch my checkbox and display a checked checkbox?

Scenario: Minimal code example necessary create.blade.php

<input type="checkbox" name="vehicle" value="Bike">Has a bike<br>
<input type="checkbox" name="vehicle" value="Car">Has a car<br>
<input type="checkbox" name="vehicle" value="Motorcycle">Has a Motorcycle
0 likes
6 replies
tykus's avatar

A group of checkboxes with the same name often hints at a many-to-many relationship - so you might want to consider that adding a vehicles table (if you don't have it already) and a user_vehicle pivot table to store the relationship.

Whenever you are expecting to be able to select multiple checkboxes, you need to send an array of selected checkboxes, therefore, the name of the input field is appended with square brackets:

<input type="checkbox" name="vehicle[]" value="Bike">Has a bike<br>
<input type="checkbox" name="vehicle[]" value="Car">Has a car<br>
<input type="checkbox" name="vehicle[]" value="Motorcycle">Has a Motorcycle

Now in your controller's store method, you will have an array for the selected options:

$request->vehicle // => array(0 => "Bike", 1 => "Motorcycle")

Lastly, if you chose the vehicles and user_vehicles tables as your preferred approach, you would be returning the relevant vehicle id's rather than the string values, then you can use the relationship's syncmethod to persist the selection (assumes you have a vehicles hasMany relationship on the User model:

<input type="checkbox" name="vehicle[]" value="1">Has a bike<br>
<input type="checkbox" name="vehicle[]" value="2">Has a car<br>
<input type="checkbox" name="vehicle[]" value="3">Has a Motorcycle
public function store(Request $request)
{
    $user = App\User::create($request->only('name', 'age'); // or whatever the parent in the relationship is

    $user->vehicles()->sync($request->vehicle);
}
5 likes
michaelrtm's avatar

You haven't really given a lot of information. Are these vehicle choices the only choices, or might you one day need a boat choice? Are vehicles a Model in your system, or is this just for a profile?

I'd probably have a pivot table user_vehicles with user_id and vehicle_type - gives it room to grow.

Your other option is to store it as a string on whatever table you are storing it on. You would put $table->string('vehicles') in your migration and then store the selected vehicles with something like $user->vehicles = implode(",", Input::vehicle); and retrieving with something like $vehicles = explode(",", $user->vehicles);. This is gross though, and I would be avoiding it.

Nick385's avatar

@tykus @michaelrtm First off Thanks for the response! :D I did some digging and came back with follow up questions.

@michaelrtmAre vehicles a Model in your system, or is this just for a profile? both cases could be.

The Pivot Tablesource i used at the bottom: What i understand is, its used to create a "link" between tables. This works something like vehicle model > return $this->belongsToOne('App\Employee'); the difference is that with a pivot table your more flexible...? Let's say i have four tables employee employee_location employment_type vehicle_type

relationships

return $this->belongshasMany('App\employee');
return $this->belongshasMany('App\employee_location');
return $this->belongshasMany('App\employment_type');
return $this->belongshasMany('App\vehicles_type');

pivot table employee_employee_location_employment_type_vehicle_type

$table->integer('employee_id');
$table->integer('employee_location_id');
$table->integer('employment_type_id');
$table->integer('vehicle_type_id');

employeesController

// dont know if this is correct
public function store(Request $request)
{
    $employee = App\Employee::create($request->only('name', 'address'); 

    $employee->employee_location()->sync($request->employee_location);
    $employee->employment_type()->sync($request->employment_type);
    $employee->vehicle_type()->sync($request->vehicle_type);
}

// no clue
public function show(Request $request)
{
   // no idea how i can get the right info here
}

employee

$table->increments('id');
$table->string('name');
$table->string('address'); // < multiple personal info etc.

<input type="text" name="name"> // < multiple personal info etc.

employee_location

$table->increments('id');
$table->integer('employee_id');
$table->boolean('office');
$table->boolean('remote');
$table->boolean('hybrid');

<input type="checkbox" name="employee_location[]" value="1">office<br>
<input type="checkbox" name="employee_location[]" value="2">remote<br>
<input type="checkbox" name="employee_location[]" value="3">hybrid

employment_type

$table->increments('id');
$table->integer('employmee_id');
$table->boolean('fulltime');
$table->boolean('partime');
$table->boolean('contract');

<input type="checkbox" name="employment_type[]" value="1">fulltime<br>
<input type="checkbox" name="employment_type[]" value="2">partime<br>
<input type="checkbox" name="employment_type[]" value="3">contract

vehicle_type

$table->increments('id');
$table->integer('employmee_id');
$table->boolean('bike');
$table->boolean('car');
$table->boolean('motorcycle');

<input type="checkbox" name="vehicle_type[]" value="1">Has a bike<br>
<input type="checkbox" name="vehicle_type[]" value="2">Has a car<br>
<input type="checkbox" name="vehicle_type[]" value="3">Has a Motorcycle

pivot source: http://laraveldaily.com/pivot-tables-and-many-to-many-relationships/

michaelrtm's avatar

An employee is either full time, part time or contract - that can't be mixed.

An employee can only work remotely, in the office or Hybrid - that can't be mixed.

They should be hasOne / belongsTo relationships. These don't need a pivot, just a single column on your employee table.

Notes: I would personally name the models vehicle and location and employment, as I feel like '_type' is superfluous and makes the code harder to read, but that's your choice. For the sake of readability, I've dropped 'type' in these examples

Also, as soon as a table is named employee_employee_location_employment_type_vehicle_type you really need to rethink what you are doing - your code shouldn't really ever be so hard to read that it is a tounge twister to read out loud

Relationships

Employee belongsToMany Vehicle
Employee hasOne Location // is really only one of those choices?
Employee hasOne Employment // 

Vehicle BelongsToMany Employee
Location belongsTo Employee
Employment belongsTo Employee

Employee Table

$table->integer('id');
//other data
$table->unsignedInteger('location_id');
$table->unsignedInteger('employment_id');

Vehicle / Location and Employment type Table

$table->integer('id');
//other data
$table->string('type');

Vehicle checkboxes

@foreach($vehicles as $vehicle)
    <input type="checkbox" name="vehicle[]" value="{{ $vehicle->id }}">Has a {{ $vehicle->type }}
@endforeach

Location / Employment radio buttons (single choice)

@foreach($locations as $location)
    <input type="radio" name="location" value="{{ $location->id }}">{{ $location->type }}
@endforeach

@foreach($employments as $employment)
    <input type="radio" name="employment" value="{{ $employment->id }}">{{ $employment->type }}
@endforeach

employee_vehicle Pivot Table

$table->integer('vehicle_id');
$table->integer('employee_id');

EmployeeController

public function create()
{
    $locations = Location::get();
    $vehicles = Vehicle::get();
    $employments = Employment::get();

    //return view with those variables
}

public function store(Request $request)
{
    $employee = App\Employee::create($request->only('name', 'address'); 

    $employee->location_id = $request->location;
    $employee->employment_id = $request->employment;
    $employee->vehicles()->sync($request->vehicle_type);
    $employee->save();

    //redirect etc
}

// you shouldn't use Request $request here, you should use an id, or Model Binding
public function show($id)
{
   $employee = Employee::find($id);
   //return view with employee info 
}

Finally, the employee/show.blade.php

<p>Name: {{ $employee->name }}</p>
<p>Location: {{ $employee->location->type }}</p>
<p>Employment Status: {{ $employee->employment->type }}</p>
<p>Vehicles :</p>
<ul>
    @foreach($employee->vehicle as $vehicle)
        <li>{{ $vehicle->type }}</li>
    @endforeach
</ul>
michaelrtm's avatar

I've chosen to make everything Models, as it allows for options - eg you might need to add 'International' to location one day or Volunteer to employment.

Managing those can be a simple CRUD option in your admin panel, or you can hard code it into your database and seed those options when you go to production.

mojtabanaemi's avatar

Hello , I have two checkbox for male and female and store it in database in two column male and female now I want to show from database if it was male it show male and if it was female it will show female , I use compact function for showing because I have photo also . tanks

Please or to participate in this conversation.