// in my blade
<x-button.button wire:click='$dispatch("open-create-class-modal")' >
{{ __('Create Class') }}
</x-button.button>
// in my component
#[On('open-create-class-modal')]
public function openModal()
{
$this->showModal = true;
}
<?php
namespace App\Livewire\Classes;
use Livewire\Component;
use Livewire\Attributes\On;
use App\Livewire\Forms\ClassForm;
class ClassCreate extends Component
{
public ClassForm $form;
public $departments;
public $showModal = false;
#[On('open-create-class-modal')]
public function openModal()
{
$this->showModal = true;
}
public function mount()
{
$this->departments = \App\Models\Department::all();
}
public function createClass()
{
$this->form->store();
session()->flash('flash', ['type' => 'success', 'message' => 'Class created!']);
}
public function render()
{
return view('livewire.classes.class-create');
}
}
Thank you for your help! I finally got the modal to pop up by switching from dispatching the event from the blade template to dispatching the event to the component.
// app/Livewire/Classes/ClassIndex.php
public function showModal()
{
$this->dispatch('open-create-class-modal')->to(ClassCreate::class);
}
// app/Livewire/Classes/ClassIndex.php
#[On('open-create-class-modal')]
public function openModal()
{
$this->showModal = true;
}