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

skoobi's avatar
Level 13

Save Multiple Grouped checkbox array to database

Hi. Im completely baffled on how to save multiple groups of arrays to a database or even if its possible.

What im trying to do is

@foreach($customers as $customer)
<input type="checkbox" name="activity_1[]" value="1">
    Pre-Book this for <strong>{{ $customer->name }}</strong>
@endforeach 


@foreach($customers as $customer)
<input type="checkbox" name="activity_2[]" value="1">
    Pre-Book this for <strong>{{ $customer->name }}</strong>
@endforeach 

@foreach($customers as $customer)
<input type="checkbox" name="activity_3[]" value="1">
    Pre-Book this for <strong>{{ $customer->name }}</strong>
@endforeach 

So there could be 1-3 customers in each activity.

I want to do it so it saves to the database as a relationship

So the database is like this::

customer 1

  • id = 1
  • customer_id = 1
  • activity_1 = true
  • activity_2 = false
  • activity_3 = true

customer 2

  • id = 2
  • customer_id = 2
  • activity_1 = true
  • activity_2 = true
  • activity_3 = true

Any help would be greatful. I have tried this::

$activities = $request->activity_1;
$activities_2 = $request->activity_2;
$activities_3 = $request->activity_3;

foreach ($activities as $index => $activity) {
    $activity = new Activity();
    $activity->activity_1 = $activity;
    $activity->activity_2 = $activity_2[$index];
    $activity->activity_3 = $activity_3[$index];
    $activity->save();
}

but i get 0 in the columns. but if i return the $activities array it shows that its ok.

0 likes
1 reply
skoobi's avatar
Level 13

Never mind....

Serialised it all to the database. Not ideal as i wanted to do them separate and have a relationship there.

Please or to participate in this conversation.