Stringify checkbox prefilled values?

What seems to be the problem:

public $selectedValues = [];

public function mount() {
    $this->selectedValues = $databaseModel->pluck('id')->map(function($id) {
        return (string) $id;
    })->all();
}

When not making it a string the checkboxes are keep coming back with the ‘checked’ values. Also when not checked in the UI.

Steps to Reproduce:

// $types is an Eloquent collection here
@foreach ($types as $type)
    <input type="checkbox" wire:model="selectedValues" value="{{ $type->id }}" /> {{ $type->description }}
@endforeach

When clicking the checkboxes (not the ones in the $selectedValues) everything goes well, when clicking the $selectedValues, they are coming back to the default you set in the component class itself.

Are you using the latest version of Livewire:
No, 2.2.8 I am using.

Do you have any screenshots or code examples:

Hey, @edwinheij
it seems you question is not complete, first what is the $types variable and what you are trying to do exactly.

You have an extra </label> closing tag inside your foreach
Can you give more explanation please?

I updated the question. Thanks for the comment.

hi @edwinheij,
I am not sure what exactly causing the problem of keeping checkboxes checked Livewire or browser, but you may try to initialize unselected values with null in $selectedValues[]. If your checkboxes are changing based on some inputs it might make sense to add names or ids to the inputs it will help (agin not sure who is guilty Livewire or browser) to differentiate checkboxes and not keep “old” values.

Is there any relation between $databaseModel and $types?
What $type stands for?

I’m not sure about your Structure exactly but I can give you an Idea how to do things (in my perspective).

If there is any relation between your model and $types (e.g: roles and permission) this is another Idea, if Not you can just set a boolean column inside the $types table to check if the input was checked or not.

And if it’s not the Idea you can explain more, As @Dimonka2 said make sure this is not a browser cache fault or something like that.

  • Try to clear browser cache
  • Try to test your app in another browser
  • make sure your assets (LivewireScripts, LivewireStyles) in the right place
  • if you’re upgrading your application from LV1 to LV2 try to republish your assets once again
    • php artisan view:clear and php artisan livewire:publish --assets
  • Follow the Request in Network Tab and see the response
  • Debug Each Peace of your code separately to make sure everything is worked as expected.

I do not think it is a “cache”, it is more likely some kind of auto fill “intelligence”

Did you find what he’s trying to accomplish here ? Cause Until now I don’t know what’s he trying to do exactly

Not very sure either. Another thing that is not clear from the code above is how the checkbox states are saved…

Exactly, need more info

I’ve added a video for explanation

@calebporzio explained this issue in the datatable series which came out this week @ https://laravel-livewire.com/screencasts/s7-select-all.
You have to map id’s to strings to work around his. Nice to know that I was on the right track.

This is exactly what I suggested to you in my first post :wink: