sergionc
1 month ago
424
2
General

select dynamic with livewire

Posted 1 month ago by sergionc

Hello friends, I am trying to do dynamic select using livewire, any help?

Component:

public $ciudades=[];

public function render()
{
    $regiones = Region::all();
    return view('livewire.select-ciudad',compact('regiones'));
}

public function showciudades($id)
{
    $this->ciudades = Ciudad::where('region_id', '=',$id)->get();
}

View, select father :

    <select class="form-control" style="width: 100%">
        @foreach ($regiones as $region)
            <option value="{{ $region->id }}" wire:click="showciudades({{$region->id}})">
                {{ $region->nombre_region }}
            </option>
        @endforeach
    </select>

Select son:

    <select name="ciudad_id" class="form-control" style="width: 100%">
        @foreach($ciudades as $ciudad)
        <option value="{{$ciudad->id}}">{{$ciudad->nombre_ciudad}}</option>
        @endforeach
    </select>

Please sign in or create an account to participate in this conversation.