Weave 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 the missing relationship events to Laravel Eloquent models. Traits for hasOne, belongsTo, hasMany, belongsToMany, morphOne, morphTo, morphMany, morphToMany, and morphedByMany let you listen for saved, updated, attached, detached, and other events, useful for cache invalidation and audit logs.

View on GitHub
Deep Wiki
Context7

Laravel Relationship Events adds the missing relationship events to Laravel Eloquent, complementing the framework's built-in model events. By including a trait on your model, you can listen for actions performed on relationships—such as saved, updated, created, attached, detached, and synced—directly in your model's boot method.

  • Trait-based setup for HasOne, HasMany, BelongsTo, BelongsToMany, and all polymorphic variants.
  • Named listeners like hasOneSaved, hasOneUpdated, and morphToManyAttached.
  • Callbacks receive the parent model and related models or IDs.
  • Compatibility from Laravel 6.x through 13.x across PHP ^7.2 and ^8.x.
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.
htinlynn/genius-helper
maco-studios/hephaestus
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator