- How do I listen for relationship changes like `attach()` or `sync()` in Laravel?
- Use the provided traits (e.g., `HasBelongsToManyEvents`) in your model and register event listeners in the `boot()` method. For example, `static::belongsToManyAttached()` triggers when records are attached to a many-to-many relationship, with `$parent` and `$ids` parameters for context.
- Does this package support polymorphic relationships (e.g., `morphToMany`)?
- Yes, it includes traits like `HasMorphToManyEvents` and `HasMorphManyEvents` to listen for polymorphic relationship events. Events like `morphToManyAttached` or `morphManySaved` provide access to both the parent and related models, including polymorphic type and ID.
- Will this work with Laravel 13 and PHP 8.3?
- Yes, version `5.x` explicitly supports Laravel 13 and PHP 8.3. Always pin to a stable version (e.g., `^5.0`) to avoid compatibility issues during upgrades. Check the [compatibility table](https://github.com/chelout/laravel-relationship-events) for other versions.
- Can I use this for real-time notifications (e.g., WebSocket updates) when relationships change?
- Absolutely. Dispatch events like `hasManySaved` or `belongsToUpdated` to trigger queue jobs or broadcast WebSocket events. The package provides full access to parent and related models, making it easy to serialize data for real-time updates.
- How do I avoid N+1 queries when using relationship events?
- Events like `hasManySaved` or `belongsToManyAttached` may trigger additional queries if you fetch related collections. Mitigate this by eager-loading relationships (e.g., `$model->load('relation')`) before the event fires, or use caching for frequently accessed data.
- Is there a performance overhead compared to native Eloquent hooks?
- Yes, relationship events introduce minimal overhead due to additional checks and context gathering. However, the impact is negligible for most use cases. Profile with tools like Laravel Debugbar to ensure it fits within your performance budget.
- Can I use Laravel Observers instead of model listeners for these events?
- Yes, the package works seamlessly with Laravel Observers. Register observers in `AppServiceProvider@boot()` to handle events centrally (e.g., for auditing or analytics) without cluttering individual models. Observers receive the same `$parent` and `$related` context.
- What if I only need coarse-grained events (e.g., any relationship update) instead of fine-grained ones?
- You can aggregate events by dispatching a custom event (e.g., `RelationshipUpdated`) from within the trait’s event handlers. This reduces boilerplate while maintaining flexibility for complex workflows.
- How do I test relationship events in PHPUnit/Pest?
- Use Laravel’s event testing helpers (e.g., `expectsEvents()`) or mock the event dispatcher. For unit tests, create test doubles for event handlers and verify they receive the correct `$parent` and `$related` models. Integration tests should simulate relationship operations (e.g., `attach()`, `save()`).
- Are there alternatives to this package for relationship events in Laravel?
- Native Eloquent lacks relationship-specific events, but you could build custom observers or use packages like `spatie/laravel-activitylog` for auditing. However, this package provides standardized, type-safe events for all relationship types (1:1, 1:N, N:M, polymorphic) with minimal setup.