Home > Back-end >  wire:change on select in livewire
wire:change on select in livewire

Time:11-10

I want to trigger a function in livewire when an option is selected in dropdown. But with wire:click or wire:change dd in function not working. Is there a wire:bla bla for this in livewire?

<x-select name="course_id" label="{!! __('Ders') !!}" wire:model="course_id" wire:change="courseSelected" id="course_id"  :options="$this->courses"/>
 public function courseSelected(){
        dd("here");
}

CodePudding user response:

you should hook to the updated method in the lifecycle.

https://laravel-livewire.com/docs/2.x/lifecycle-hooks

public function updatedCourseId(){
   dd("here");
}

Also remove wire:change

CodePudding user response:

if you bind the element to property using wire:model, you can hook the lifecycle as mentioned above

public function updatedCourseId($value)
{
    dd($value);
}

in a different approach, you can listen for the event using wire:change, and for that, you must define the event in listener property like

<x-select label="{!! __('Ders') !!}" wire:change="$emit('courseSelected', $event.target.value)" id="course_id"  :options="$this->courses"/>

// in component
protected $listeners = [
   'courseSelected'
];

public function courseSelected($value)
{
   dd($value);
}
  • Related