Skip to main content

What are Model Events?

Model events are hooks that fire automatically during a model’s lifecycle. They let you run custom code when models are created, updated, deleted, or restored.
Real-world analogy: Model events are like automatic triggers in your daily routine. When you wake up (event), your coffee maker starts brewing (hook). When you leave home (event), your security system activates (hook). Events happen automatically, and hooks respond to them.

Available Events

Basic Event Usage

Defining Events in Models

Event with Conditions

Advanced Event Patterns

Conditional Events

Async Event Handling

Error Handling in Events

Event Context and Data

Accessing Original Data

Event Data Helpers

Global Events

Model-Specific Global Events

Cross-Model Events

Event-Driven Architecture

Event Dispatching

Queue-Based Events

Testing Events

Mocking Events

Event Testing Helpers

Performance Considerations

Async Event Optimization

Event Batching

Best Practices

1. Keep Events Focused

2. Handle Errors Gracefully

3. Use Appropriate Event Types

4. Document Event Side Effects

Next Steps

Model Observers

Organize event logic with observer classes

Custom Casting

Transform data with custom cast classes