Checkbox keeps the old values livewire

checkbox keeps the old values it doesn’t refresh after submit

It’s a bit difficult to determine what the issue is without more of your code’s context. I set up a simple test with a @foreach loop and checkboxes. The checkboxes persisted the data in my example. I’m not sure why you’re setting the value as “answers.{{ $index }}”. That would only serve to convey that the value of the checkbox would be “answers.0”, “answers.1”, etc… If your answers are an associative array that you’re trying to set the value on, you’ll need to do something like value="{{ $answers[$index] }}", but that doesn’t feel right either as you’re modeling against $answers. I’d assume you’re keeping your values in the $options array so your value should be set as value="{{ $option }}".

Try setting up a simpler test component to verify that the checkboxes are working for you. This is what my test setup looks like:


class TestComponent extends Component
    use WithFileUploads;

    public $options = [

    public $answers = [];

    public function mount()


    public function test()
        // this is the function called by the form submission

    public function render()
        return view('livewire.test-component');


    <form wire:submit.prevent="test">
    @foreach ($options as $index => $option)
        <div>{{ $answers[$option] ?? '' }}
            <input wire:model="answers.{{ $option }}" value="{{ $option }}" type="checkbox">
1 Like

Thanks for your reply ,i hope you understand me and Thanks
i have in my database table questions and i have two attributes options[] and correct_answers[] when i create the first question it’s stock fine in database

but when i want to create the second question the checkbox doesn’t clear their first attribute

So i need to clear the checkbox but it’ didn’t work

Okay, I see what’s happening here. During the DOM diffing, livewire is seeing the checkbox and seeing that it functionally has nothing different than the checkbox being returned from the server so it skips over it. Even though it’s “checked”, it isn’t reflected as “checked” in the HTML. You can force livewire to swap out the component HTML by appending a random id to the checkbox.

Where you have:
<input wire:model="answers.{{ $option }}" value="answers.{{ $index }}" type="checkbox">

Try this instead:
<input wire:model="answers.{{ $option }}" value="answers.{{ $index }}" type="checkbox" id="{{ rand() }}">

That should force it back to unchecked when it’s swapped out on DOM diff. You could also use something more relevant to your script as an id but it should be unique and different from the previous version. The thing to remember is that if it isn’t different from the original version, it won’t be swapped out.

1 Like

thanks a lot :+1: :grinning:

Can you help me ? I want to store just the correct answers not all in the database
because if i check an option and then i unchecked , it store in database the false answers

Before storing to the database, to a foreach loop on the $answers and unset anything that is false.

1 Like

thank u :grinning: :ok_hand: