Search code examples
laravellaravel-livewire

How to pass data to livewire modal component?


I have category component with add button as below.

<button wire:click.prevent="addNew" class="btn btn-primary mb-2">Add</button>
  .....
  .....
<x-mymodal-component :iscreate="$iscreate"/>

with 'addNew' method, I dispatch event to javascript listener.

public function addNew(){
    $this->dispatchBrowserEvent('show-category-modal');
}

In the listener event, I try to show modal that made as component.

  window.addEventListener('show-category-modal', event => {
    $('#category-form').modal('show');
  });

Below is modal component with data that need to pass into.

<x-mymodal-component :iscreate="$iscreate"/>

Any advice or guidance on this would be greatly appreciated, Thanks.


Solution

  • @livewire('mymodal-component', ['iscreate'=>$iscreate])
    
    //Receive:
     public function mount($iscreate)
      {
    
      }