'Laravel/Livewire differentiate two dropdown menus in of the same model

Context: I have a category with subcategories; these categories load using a foreach in my Blade file. These subcategories have a dropdown to select data from them. My problem is that when I change the dropdown of one of the subcategories, the other subcategories change too.

@foreach ($subcategories as subcategory )

<x-simple-select wire:model="subcategory_id" :wire:key="$subcategory->id"
    name="subcategory" id="{{$subcategory->id}}"
    :options='$types' value-field='subcategory' text-field='subcategory'
/>

@endforeach

I want to save the selected data from the dropdown using updatedSubcategoryId($value), but I don't know how to save it since all the subcategories are changed and not just one.



Solution 1:[1]

ok I figured out to do.the all idea was wrong (i'm new in livewire). In case it serves to someone:

<form wire:submit.prevent="save()">

  @foreach ($subcategories as $key => $subcategory )

    <x-simple-select wire:model.defer="subcategories.{{$key}}.id" :options='$types' />

  @endforeach

  <button type="submit">save</button>

</form>

and then in the controller:

protected $rules = [
  'subcategories.*.id' => '',
  ];

public function save()
{
  $this->validate();
  foreach ($this->subcategories as $subcategory) {
  $subcategory->save();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Juan Matias Granda