Appearance
Event
Naming Conventions
Structure: Subject + PastTenseVerb (e.g., OrderPaid, UserCreated)
php
// ✅ Good
class OrderPlaced {}
class OrderShipped {}
// ❌ Bad
class OrderPlacedEvent {} // Don't add "Event" suffix
class PlaceOrder {} // Use past tense, not imperativeFor "before" events (allowing listeners to modify/prevent the action), use present continuous:
php
class OrderProcessing {} // Before
class OrderProcessed {} // AfterMinimize the number of traits
By default, Laravel adds few traits to a new Event class, even if it’s unnecessary in your particular case. We fixed it in our custom stub file for Event, but it’s still better to control traits more explicitly.
diff
-use Dispatchable, InteractsWithSockets, SerializesModels;
+use SerializesModels; // only if the Event will be used with Queued Event ListenersDispatchableis to add static methods to for event dispatching, likenew OrderPlaced()::dispatch().- By IxDF conventions, we do not use this syntax, so we don’t need this trait. Please use
Eventfacade instead, e.g.\Illuminate\Support\Facades\Event::dispatch(new YourEvent()).
- By IxDF conventions, we do not use this syntax, so we don’t need this trait. Please use
SerializesModelsis to gracefully serialize any Eloquent models if the event object contains Eloquent models and going to be serialized using PHP'sserializefunction, such as when utilizing queued listeners.InteractsWithSocketsis for broadcasting only, e.g. using Laravel Echo.
Best Practices
- Use business language — name events using domain terms, not technical ones (
OrderPaidnotPaymentRecordInserted) - Include all needed context — pass data listeners need; don't make them fetch extra data
- Keep events simple — events are data carriers, no business logic
- Dispatch after success — dispatch after the action completes, not before
- One event per action — don't combine unrelated changes into one event
- Event classes should be
finalandreadonly