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

CODE-AXION's avatar

how do i get selected checkboxes values in an array?

whenever i check a box i always get 1 value at a time i want to get all the selected values, i making a ecommerce application and i want this checkboxes in shop page so user can filter out without the need of submit button

so instead of working like this :

0 => 1
1 => 2 
2 => 3 //these are selected values , the more you select the more array values will be added

its working like this:

1 => 2 //only gets 1 value when checkbox is selected 

how do i bind those selected values , i have been researching this about 3 days and i have still not figured it out

any help would be much much appreciated

blade view file:- shop-component livewire :

  <div class="brand">
            @foreach ($attributes as $attribute)
           
                <h3 >{{$attribute->name}}</h3>
                <hr class="hr_category">
                @foreach ($attribute->attributeValues as $item)

                  <div class="category_sort_wrapper">
                    <input wire:model="selected" id="{{$loop->index}}" name="{{$loop->index}}" value="{{$item->id}}"  type="checkbox"> <h6 class="category_sort">{{$item->value}}</h6>
                  </div>

                @endforeach

            @endforeach

        </div>

logic file: ShopComponent


    public array $selected = array();



    public function mount()
    {
  	    $this->selected = AttributeValue::all()->pluck('id')->toArray();
    
    }

    function updatedselected()  
        {

            // wants the checkbox selected values in $this->selected array
            dd($this->selected);
            
        }


0 likes
8 replies
CODE-AXION's avatar

@frankielee still not getting selected values mate

i changed it from this :

                    <input wire:model="selected" id="{{$loop->index}}" name="{{$loop->index}}" value="{{$item->id}}"  type="checkbox"> <h6 class="category_sort">{{$item->value}}</h6>

to this :

                    <input wire:model="selected.attr" id="{{$loop->index}}" name="{{$loop->index}}" value="{{$item->id}}"  type="checkbox"> <h6 class="category_sort">{{$item->value}}</h6>

and i am getting the results like this

 array:1 [▼
  "attr" => "3"
]

is there anything to do in the component mate ?

frankielee's avatar

@CODE-AXION

Since the $selected is an array, the value after the dot should be the index

Example:

<input wire:mode="selected.0"/>
<input wire:mode="selected.1"/>

But since you are using an for loop, you should just use the $loop indexes

CODE-AXION's avatar

@frankielee i am getting like this

array:1 [▼
  1 => false
]

and i did this in blade:

                    <input wire:model="selected.{{$loop->index}}" id="{{$loop->index}}" name="{{$loop->index}}" value="{{$item->id}}"  type="checkbox"> <h6 class="category_sort">{{$item->value}}</h6>

i want the full array of selected values brother ! for eg:- like this:

  1  => false
  3 => false
  4 => true //selected values from checkbox
  6 => true //selected values from checkbox
  5 => true //selected values from checkbox
Snapey's avatar

@CODE-AXION You will only get a value when you check or uncheck the checkbox. When you check it will add it, with the value you specified, when you uncheck it then the entry in the array is set to false.

Install Debugbar, you can then inspect your livewire variables directly.

If you want to have the full array from the start then you need to create the array in the mount method.

foreach($attributes as $key => $attribute) {
	$this->selected[$key] = false;   // or whatever initial value
}
CODE-AXION's avatar

@Snapey i cant do this because i am fetching attributes from products variable which is a eloquent query in render method so i have to change my entire code system and if i did its not gonna work i have rewrite entire logic !!!

do you have any idea how can i make shop filter then ? where attribute are in checkboxes like red,green,blue color etc... and whatever value he selects he gets the the products he wants

$products::whereHas('attributeValues' function($query){

$query->whereIn('id', $this->selected));

})->where('category_id',3)->orwhere('category_id',3)->orwhere('category_id',3);
Snapey's avatar
Snapey
Best Answer
Level 122

@CODE-AXION You have been advised how to work with an array. If you can't initialise the checkboxes on mount then you will need to deal with the fact that the array will only tell you the checked checkboxes and the others wll need to assume to be false.

1 like
CODE-AXION's avatar

@Snapey now i got it , i think you are saying is right that its only showing me the checked ones and i have to mount those attributes if i want the full array, because when i put $this->selected in the query it works perfect but thanks for this information i didnt knew this !

Please or to participate in this conversation.