You dispatch an event but the handler never runs. Note for readers of older answers: Laravel 5.x used Event::fire(...). That method is gone from current Laravel (it does not exist on the 10.x facade). Use event(...) or Event::dispatch(...) instead.
Register and dispatch
// app/Providers/AppServiceProvider.php, in boot()
use App\Events\KitchenStored;
use App\Listeners\AttachCurrentUserToKitchen;
use Illuminate\Support\Facades\Event;
Event::listen(KitchenStored::class, AttachCurrentUserToKitchen::class);
class KitchenStored
{
use Dispatchable;
public function __construct(public string $kitchenName) {}
}
class AttachCurrentUserToKitchen
{
public function handle(KitchenStored $event): void
{
// ...
}
}
Dispatch it with any of these:
event(new KitchenStored('Main kitchen'));
KitchenStored::dispatch('Main kitchen');
Event::dispatch(new KitchenStored('Main kitchen'));
Why it does not fire
- The class string does not match.
Event::listen('App\Events\KitchenStored', ...)with a typo or wrong namespace fails silently. UseKitchenStored::classso a typo is an error.Event::hasListeners(KitchenStored::class)tells you whether anything is registered. - The listener is queued. A listener that implements
ShouldQueueis pushed to the queue and runs only when a worker is running. WithQUEUE_CONNECTION=syncit runs inline. - Cached events. If you ran
php artisan event:cache, runphp artisan event:clearafter changing listeners.
Tested on Laravel 10.x (PHP 8.1) and 12.x (PHP 8.3): the tests cover the sync listener, event() against Event::dispatch(), a mismatched class string never firing, and a queued listener being pushed instead of run. Source: tests/laravel-verification/files/q4006.