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

APPLE199's avatar

how to use {{ old('name') }} in checkbox?

So I just ditched Laravel HTML collector so I am just wondering what the best way to do form model binding is.

I have a checkbox:

<input name="use_signature" type="checkbox" value="1">

When it's checked and the validation fails with other fields in the form, `{{ old('first_name') }} works but it doesn't work for checkbox.

How can I make it auto check the checkbox if it was checked and how can I make the checkbox auto check if in database, it has been checked (say when the user is editing his account info and his "use_signature" has been checked – I want to show that the checkbox has been checked automatically).

thanks!

0 likes
8 replies
michaelmcmullen's avatar

There is probably a better way but you might be able to use something like

<input type="checkbox" name="use_signature" value="1" {{ (! empty(old('use_signature')) ? 'checked' : '') }}>

Or something like that, sorry didn't get to test it; just came to my mind.

2 likes
spekkionu's avatar
<input type="hidden" name="use_signature" value="0">
<input type="checkbox" name="use_signature" value="1" {{ old('use_signature') ? 'checked' }}>

If this is an edit form pass the value from the model as the second parameter for the old function.

The hidden input is a trick I like to use to make handling checkboxes easier.
Normally for a checkbox the value is submitted if the box is checked and nothing at all is submitted if it is not.
This makes you have to use things like isset or !emptywhen looking at checkbox values as the key wont exist in the request if the box wasn't checked.

With the hidden input the key will always be submitted with either 1 if checked or 0 if not checked.

4 likes
APPLE199's avatar

@spekkionu Thanks, that's what I do too; create 2 inputs.

But if I pass the second parameter in the old function, I will get an error when using the same form for "creating" as it will only work for "editing"

RoboRobok's avatar

Life would be easier if browsers were sending unchecked checkboxes with empty value.

1 like
thomaskim's avatar

What do your controllers look like that you guys feel the need to add hidden input fields? To me, it just looks like you are unnecessarily complicating things..

spekkionu's avatar

But if I pass the second parameter in the old function, I will get an error when using the same form for "creating" as it will only work for "editing"

In the controller that shows the add form just do a new ModelName() to create a blank instance of the model and pass it to the view. Then the view should look the same for the add and the edit form.

iscromanpc's avatar

@spekkionu Your solution works for me.

<input type="checkbox" name="accepted_terms_cond" value="1" {{ old('accepted_terms_cond') ? 'checked' : null }}> I have read and accept the terms and conditions.

Thanks.

Gum's avatar

I'm late to the party but I just found this gem. Tested in Laravel 8.

   {{
         session()->hasOldInput()
            ? (old('check') !== null ? 'checked' : '')
            : ($databaseIsChecked ? 'checked' : '')
   }}
2 likes

Please or to participate in this conversation.