Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Livewire how to $emit event on select change (wire:model)

Livewire how to $emit event on <select> change (wire:model)

I need to fire event (fetch some data from DB in another component) on simple <select> change.

<select id="hall" wire:model="hall_id">...</select>

How to watch changes for this model? On VueJS we just set $watch or $computed properties, I believe in livewire should be something similar. It's strange why there is no wire:change directive.

This is how I'm trying to emit event now:

<?php

namespace App\Http\Livewire;

use App\Models\Event;
use App\Models\Hall;
use Livewire\Component;

class ShowReservationForm extends Component
{
    public $hall_id = '';

    protected $queryString = [
        'hall_id' => ['except' => ''],
    ];

    public function mounted()
    {
        //
    }

    public function updatedHallId($name, $value)
    {
        $this->emit('hallChanged', $value);
    }

    public function render()
    {
        return view('livewire.show-reservation-form', [
            'halls' => Hall::all(),
        ]);
    }

    public function getHallEventsProperty()
    {
        return Event::where('hall_id', $this->hall_id)->get();
    }
}

and catch it:

<?php

namespace App\Http\Livewire;

use Livewire\Component;

class ShowReservations extends Component
{
    protected $listeners = ['hallChanged'];

    public $showTable = false;

    public function render()
    {
        return view('livewire.show-reservations');
    }

    public function hallChanged()
    {
        $this->showTable = true;
    }
}

Must be missing something obvious.

like image 237
RomkaLTU Avatar asked Oct 05 '20 06:10

RomkaLTU


People also ask

How do I transfer data from one component to another in livewire?

You can pass data into a component by passing additional parameters into the <livewire: tag. For example, let's say we have a show-post component. Here's how you would pass in a $post model. Alternatively, this is how you can pass in parameters using the Blade directive.

How do you call a component on a button click in livewire?

There are 3 livewire components UserIsExpired , UserIsActive and UserIsPending and 3 buttons respective to each component. When a button is clicked, it should replace previous component with its respective component. When Active button is clicked, it should load UserIsActive component. Same goes for other two.

What is wire model in livewire?

Livewire knows to keep track of the provided name because of the wire:model directive. Internally, Livewire listens for "input" events on the element and updates the class property with the element's value. Therefore, you can apply wire:model to any element that emits input events.


2 Answers

use wire:change

<select id="hall" wire:model="hall_id" wire:change="change">...</select>

then in component

In ShowReservationForm.php

public function change()
{
     $this->emit('hallChanged'); 
}

then you can listen it on ShowReservations component ref link https://laravel-livewire.com/docs/2.x/events

in ShowReservations.php

 protected $listeners = ['hallChanged' => 'change'];

public function change()
{
   $this->showTable = true;
}
like image 91
Kamlesh Paul Avatar answered Sep 20 '22 09:09

Kamlesh Paul


It's turns out that when receiving event, property value must not be bool?

This will not work:

public $showTable = false;
...
public function hallChanged()
    {
        $this->showTable = true;
    }

This will work

public $showTable = 0;
...
public function hallChanged()
    {
        $this->showTable = 1;
    }
like image 35
RomkaLTU Avatar answered Sep 21 '22 09:09

RomkaLTU