skip to Main Content

In the function "add" at some point it throws and event to the component "shopping-cart". In the "shopping-cart" component, i want to refresh the component manually when it receives the event. On the livewire site it only shows how to do it on the component view, by clicking a button and livewire handles the rest, but those function seems to not be available in the backend. Is the anyway around this or am i missing something??

//

// on the shopping-cart component
#[On('basketUpdated')]
public function basketUpdated(): void
{
    // something here
}

// Adding more products to the Basket;
public function add($value): true|LaravelNotify
{
    // Attempt to add the product to the use Basket DB
    $addProductToBasket = $this->includeInBasket($value);

    // checking for error
    if (!$addProductToBasket)
    return notify()->error('you are not connected, please connect and try again!', 'Could not update basket');

    // Event to refresh shopping cart component
    $this->dispatch('basketUpdated')->to('shopping-cart');
    return true;
}

2

Answers


  1. Instead of using the On attribute, you can make a protected $listeners array and refresh when the event is triggered.

    // In the shopping-cart component
    protected $listeners = [
        'basketUpdated' => '$refresh'
    ];
    
    public function basketUpdated(): void
    {
        // something here
    }
    
    Login or Signup to reply.
  2. #[On('basketUpdated')]
    public function basketUpdated(): void
    {
        $this->emitSelf('refresh');
    }
    

    Update you blade with,

    <div>
        <livewire:shopping-cart wire:poll="refresh" />
    </div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search