Event System Best Practices
About 145 wordsLess than 1 minute
eventsbest-practicesfilterable
Keep listeners lightweight: Avoid heavy processing in event listeners to prevent performance degradation.
Use queued jobs for expensive operations: If you need to perform heavy tasks, dispatch a job from the listener:
Filterable::on('filterable.applied', function ($filterable, $builder) { ProcessFilterAnalytics::dispatch($filterable, $builder->toSql()); });Disable in production if not needed: If you're only using events for debugging, disable them in production:
'events' => [ 'enabled' => env('FILTERABLE_EVENTS_ENABLED', !app()->environment('production')), ],Use observers for filter-specific logic: Keep global listeners for cross-cutting concerns and use observers for filter-specific behavior.
Always flush in tests: Prevent test pollution by flushing listeners in
tearDown():protected function tearDown(): void { Filterable::flushListeners(); parent::tearDown(); }
