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

artisticre's avatar

Checkbox Inserts

I have three checkboxes below. And then I have what I think the input into the database should be but its not working. I don't think I have it right. Can someone help please? Any number can be checked except none of them. At least one needs to be checked.

        <input type="checkbox" name="clergy[]" value="Spiritual Director" />
        <input type="checkbox" name="clergy[]" value="Lay Team" />
        <input type="checkbox" name="clergy[]" value="Wherever Needed" />

            $teamapp->clergy = $request->input('clergy[]');

0 likes
8 replies
artisticre's avatar

I have looked over these tutorials and to be honest I really do not understand what to do for checkboxes. Radio buttons I get, not multi-answer checkboxes.

jlrdw's avatar

Remember a checkbox if nothing is set nothing is posted.

You Loop through the request and get the ones that were checked and store them.

Come edit time it's the reverse you have to loop to compare what was stored and show checked as needed.

1 like
Snapey's avatar

name each checkbox rather than using an array, and forget the values. You can then build rules and persist the states more easily

1 like
a4ashraf's avatar

Hi @artisticre

Try this


        $teamapp->clergy = collect($request->input('clergy'))->toJson();

    // Or

    $teamapp->clergy = collect($request->clergy)->toJson();

1 like
jlrdw's avatar
jlrdw
Best Answer
Level 75

Just loop, or single checkboxes.

When you $request->input('clergy'), it's an array.

If you have 4 choices a,b,c,d.

If user checked b,d, then loop, get b.d, store in your related table, or store as json.

Honest, this has been covered many times, either store as related data in a related table or store a json array or a comma separated list.

$testarray = $request->input('clergy');
$mydata = implode(",", $testarray);

Store ---- done

1 like
Daniel_24bu's avatar

when you do $mydata = implode(",", $testarray); do you now get to $x = $mydata[5]??? in case you are using selectbox control..

Please or to participate in this conversation.