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

rokka's avatar
Level 1

Store unchecked state of checkbox

Hi,

I have a form with a checkbox

<input type="checkbox" name="is_deletion_required" id="is_deletion_required" class="form-checkbox rounded-md" value="1" @if(old('is_deletion_required',$computer->is_deletion_required)=="1") checked @endif />

And an UpdateComputerRequest

class UpdateComputerRequest extends FormRequest
{
    public function rules()
    {
        return [
            'donor' => ['nullable', 'string' ],
            'email' => ['nullable', 'email' ],
            'model' => ['nullable', 'string' ],
            'comment' => [ 'nullable' ],
            'is_deletion_required' => [ 'boolean' ],
            'needs_donation_receipt' => [ 'nullable', 'boolean' ],
            'has_webcam' => [ 'nullable', 'boolean' ],
            'required_equipment' => ['nullable', 'string' ],
        ];
    }

    public function authorize()
    {
        return true;
        //return Gate::allows('task_access');
    }
}

But if I open the form for an entry which was checked before and submit the form with an unchecked checkbox this information is not stored in the database. It remains checked on the page reload.

What am I doing wrong?

0 likes
12 replies
chaudigv's avatar
chaudigv
Best Answer
Level 16

Unchecked is not passed in the request. You will have to check for it's existence

if($request->has('is_deletion_required')) {
	$computer->is_deletion_required = true;
} else {
	$computer->is_deletion_required = false;
}
Snapey's avatar

@michaloravec difference is that your solution is cleverer since it does not require an if/else

but sometimes people don't want clever, they want obvious.

MichalOravec's avatar

Yeah I know, has() method return true or false.

But obviously people don't want to write a better code, like @chaudigv

$computer->is_deletion_required = $request->has('is_deletion_required');
chaudigv's avatar

@michaloravec I saw the level of @rokka, seeing the question is quite basic and a simple google search would have been sufficient to reach to a solution. The fact that he didn't do his research shows that he/she is a beginner and looking for a simple answer that doesn't require any further explanation.

As far as it comes to obviously people don't want to write a better code, like @chaudigv, from my perspective every person is growing. Learning and then re-learning is what makes a person better. So rather than saying "obviously", a good way to communicate would have been to tag respective persons and say "This is much shorter and clever". That's it.

There have been multiple cases where my answers were much efficient than the accepted one. That doesn't give me the right to put the person on pedestal.

I hope my response didn't cross any boundries, I am here on Laracasts to learn from others, especially from experienced developers like you.

MichalOravec's avatar

@chaudigv The word obviously is on the right place in your case and don't take it as an insult.

Your code is basically like this logic

$condition = true;

if ($condition == true) {
    $result = true;
} else {
    $result = false;
}

It's same as

$result = $condition;

In the end if somebody has a problem to understand to that logic, then programming is not for him.

MichalOravec's avatar

@chaudigv People (or beginners) usually don't think about a code, what is the biggest mistake about programming. They just copy the code and if it works, they use it all the time.

So when you gave them a code which works they don't take any effort how to do it better, or try to understand what's going on.

Which means that they stay on the same level for very long time.

So you are thinking it's better for them, but it's not.

Obviously not.

jlrdw's avatar

A ternary would be the best.

$computer = Request::has('is_deletion_required') ? 1 : 0;

Just a one liner.

Snapey's avatar

A ternary would be the best.

$computer = Request::has('is_deletion_required') ? 1 : 0;

is identical to;

$computer = $request->has('is_deletion_required');

Or did you not read any of the comments?

@jlrdw

jlrdw's avatar

@snapey I agree, I was referring to this part

    $copmuter->update($request->except('is_deletion_required') + [...

Which isn't needed.

Snapey's avatar

@jlrdw no, because your approach a) does not set the other form values, and b) does not set the is_deletion_required attribute of the model

Please or to participate in this conversation.