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

inquisitvewaffle's avatar

Save Checkbox Array to Database in Laravel 8

Hey Folks,

I am looking for advice on how to store a checkbox array from a form into a database, but rather than using json_encode or implode which put it all into a single row, I want to have each array value appear in its own row , associated with the form submission (known as an assessment in this case).

For more context, the checkboxes (Ratings) are valued 1-10 and for each form submission a user can select multiple checkboxes. With this in mind, I am thinking this is a hasMany relationship between Assessment and Ratings but not sure how this works you don't have two existing tables as with a pivot table.

Additionally, the checkbox number values are fixed so they do not exist in a table.

Happy to share Models, Views, and Controllers but wanted to put this out generally to get advice.

0 likes
5 replies
martinbean's avatar

@inquisitvewaffle Well as mentioned when you brought this up on Discord a couple of hours ago, you do have a model: rating, and your assessment would have many of them. So loop over the checked ratings and create a rating for each.

1 like
inquisitvewaffle's avatar

@martinbean fancy meeting you here! And thanks again for your help earlier!

Sorry for clogging the airways with the same question in different venues but I struggled for a few hours to implement this using the recommendation from Discord to no avail so thought it was worth posting here as well for any more recommendations.

I don't know why but this issue has been completely stumped (full disclosure I am a relatively amateur programmer as well).

For anyone else who has some thoughts here are the related models/controllers/views.

Assessment Submission Form View - https://github.com/andrewdmaclean/comtinuum/blob/windmill-ui/resources/views/livewire/assessment/submission.blade.php

Assessment Model - https://github.com/andrewdmaclean/comtinuum/blob/windmill-ui/app/Models/Assessment.php

Assessment Controller - https://github.com/andrewdmaclean/comtinuum/blob/main/app/Http/Controllers/AssessmentsController.php

martinbean's avatar
Level 80

@inquisitvewaffle I think you’re massively over-complicating the problem.

You have assessments. You want to store many ratings for a single assessment. That’s just a run-of-the-mill, has-many relation. So for each rating submitted, save a rating in the database for the associated assessment:

// $request->input('ratings') is an array of checked ratings, i.e.
// [1, 2, 5, 6]

foreach ($request->input('ratings') as $rating) {
    // Given an Assessment model, save an associated rating per checked rating
    $assessment->ratings()->create([
        'rating' => $rating,
    ]);
}
1 like

Please or to participate in this conversation.