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

Dosmukhanbet's avatar

Laravel Livewire: How to bind checkbox selected values to component array

Hello, Could anybody help me to solve following issue, I am trying to bind only selected checkbox values to array in my livewire component as below, but it does not work

Component

use Livewire\Component;
use App\Point;

class AddNewTrip extends Component
{
   
    public $selectedtypes;

    public $initial_cargo_types;;


    public function mount($cargoTypes)
    {
        $this->initial_cargo_types = $cargoTypes;
    }

}

Blade

@foreach($initial_cargo_types as $type)
       <div class="mt-1">
              <label class="inline-flex items-center">
		      <input type="checkbox" value="{{ $type->id }}" wire.model="selectedtypes"  class="form-checkbox h-6 w-6 text-green-500">
                   <span class="ml-3 text-sm">{{ $type->name }}</span>
               </label>
          </div>
 @endforeach
0 likes
11 replies
ShaneDRosenthal's avatar

I haven't played with this code, but I wouldn't set the value of the input, but maybe try wire:model="type.selectedtypes". It would help to know what data is in $this->initial_cargo_types.

laracoft's avatar

@dosmukhanbet don't know if i'm too late.

<form>
@foreach($initial_cargo_types as $type)
       <div class="mt-1">
              <label class="inline-flex items-center">
		      <input type="checkbox" value="{{ $type->id }}" wire.model="selectedtypes"  class="form-checkbox h-6 w-6 text-green-500">
                   <span class="ml-3 text-sm">{{ $type->name }}</span>
               </label>
          </div>
@endforeach
</form>
flip's avatar

What's different in this compared to original?

dariusdxd's avatar

You need to use wire:model="selectedtypes.{{ $type->id }}"

<form>
@foreach($initial_cargo_types as $type)
       <div class="mt-1">
              <label class="inline-flex items-center">
		      <input type="checkbox" value="{{ $type->id }}" wire:model="selectedtypes.{{ $type->id }}" class="form-checkbox h-6 w-6 text-green-500">
                   <span class="ml-3 text-sm">{{ $type->name }}</span>
               </label>
          </div>
@endforeach
</form>
7 likes
flip's avatar

I'm stuck with a very similar issue. How would you setup the mount() method using the nested data binding?

kreierson's avatar

You need to do wire:model instead of wire.model

Simple typo, happens to me all the time.

1 like
lyndonjohn's avatar

This might be too late.

Blade, change wire.model to wire:model

@foreach($initial_cargo_types as $type)
       <div class="mt-1">
              <label class="inline-flex items-center">
		      <input type="checkbox" value="{{ $type->id }}" wire:model="selectedtypes"  class="form-checkbox h-6 w-6 text-green-500">
                   <span class="ml-3 text-sm">{{ $type->name }}</span>
               </label>
          </div>
 @endforeach

Livewire class, declare $selectedtypes with an empty array

public $selectedtypes = [];

public function submitForm()
{
    // save selectedtypes here.
    // in my case I use sync
}
4 likes
eduardichim93's avatar

I feel like most of the answers are not complete and are vague, which needs figuring out.

You do have to use wire:model with the ":" between wire and model as it's in the documentation, you DO NOT need a form unless you want to use submit event, since you have there property you can do a wire:click on a button for the event or whatever to handle the data, but that is besides the point.

What you want is to prefill the checkboxes as an associative array with the key as your logic unique identifier such as above $type->id and the value a boolean if you want to be either checked or unchecked, in my case I want them all checked and select the key in your view by looping over your stuff:

// componet class
...
    public array $checkboxes;
	public $manyStuff;

    public function mount(Model $model)
    {

        // get an array of ids
        $setOfIds = $model->manyStuff->pluck('id')->toArray();

		//  ---- PREFILL!!! ----
		// use the ids as the keys
		// fill the values with true so all the checkboxes are checked
		$this->checkboxes = array_fill_keys($setOfIds, true);

        // set the many stuff for the loop
		$this->manyStuff = $model->manyStuff;
    }
...

// view

...
@foreach($manyStuff as $stuff)   
 		<input type="checkbox" wire:model="checkboxes.{{ $stuff->id }}"/>
@endforeach
...

Now all you got to do in your "handle data" method is to use array_filter to remove all the falses and an array_keys to get your logic unique identifiers

8 likes
dpower's avatar

@eduardichim93 This is a brilliant solution! It's helped me a lot on my current project.

Thanks for sharing it!

Please or to participate in this conversation.