Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Relationship Events

Laravel Relationship Events Laravel Package

chelout/laravel-relationship-events

Adds missing Eloquent relationship events to Laravel models. Use simple traits (HasOne/Many, BelongsTo/Many, Morph*) to listen for attach/detach/sync, saved/updated, and other relation lifecycle hooks with parent/related context and IDs/attributes.

View on GitHub
Deep Wiki
Context7

Missing relationship events for Laravel

Frequently asked questions about Laravel Relationship Events
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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor