sameedkun / laravel-relationship-events
Missing relationship events for Laravel — actively maintained fork of chelout/laravel-relationship-events
Package info
github.com/sameedkun/laravel-relationship-events
pkg:composer/sameedkun/laravel-relationship-events
Requires
- php: ^8.3
- illuminate/container: ^12.0|^13.0
- illuminate/database: ^12.0|^13.0
- illuminate/events: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.14
- orchestra/testbench: ^10.0|^11.0
- phpunit/phpunit: ^10.5.35|^11.5.3|^12.0.1
This package is auto-updated.
Last update: 2026-05-19 15:39:06 UTC
README
Missing relationship events for Laravel — actively maintained fork of chelout/laravel-relationship-events
Note: The original package by @chelout has been inactive since 2020. This fork picks up where it left off, with Laravel 13 support and active maintenance.
Compatibility
| Package | Laravel | PHP |
|---|---|---|
v5.x |
12.x, 13.x |
^8.3 |
v4.x |
11.x, 12.x |
^8.2 |
Migrating from chelout/laravel-relationship-events
Already using the original package? Switching is seamless — no code changes required. The namespace, traits, and events are all identical.
Just swap the package name in your composer.json:
composer remove chelout/laravel-relationship-events composer require sameedkun/laravel-relationship-events
Install
- Install package with composer
composer require sameedkun/laravel-relationship-events
- Use necessary trait in your model.
Available traits:
- HasOneEvents
- HasBelongsToEvents
- HasManyEvents
- HasBelongsToManyEvents
- HasMorphOneEvents
- HasMorphToEvents
- HasMorphManyEvents
- HasMorphToManyEvents
- HasMorphedByManyEvents
use Chelout\RelationshipEvents\Concerns\HasOneEvents; use Illuminate\Database\Eloquent\Model; class User extends Model { use HasOneEvents; public static function boot() { parent::boot(); static::hasOneSaved(function ($parent, $related) { dump('hasOneSaved', $parent, $related); }); static::hasOneUpdated(function ($parent, $related) { dump('hasOneUpdated', $parent, $related); }); } }
use Chelout\RelationshipEvents\Concerns\HasMorphToManyEvents; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasMorphToManyEvents; public static function boot() { parent::boot(); static::morphToManyAttached(function ($relation, $parent, $ids, $attributes) { dump('morphToManyAttached', $relation, $parent, $ids, $attributes); }); static::morphToManyDetached(function ($relation, $parent, $ids) { dump('morphToManyDetached', $relation, $parent, $ids); }); } public function tags() { return $this->morphToMany(Tag::class, 'taggable'); } }
- Dispatchable relationship events.
It is possible to fire event classes via $dispatchesEvents property and adding HasDispatchableEvents trait:
use Chelout\RelationshipEvents\Concerns\HasOneEvents; use Chelout\RelationshipEvents\Traits\HasDispatchableEvents; use Illuminate\Database\Eloquent\Model; class User extends Model { use HasDispatchableEvents; use HasOneEvents; protected $dispatchesEvents = [ 'hasOneSaved' => HasOneSaved::class, ]; }
Relationships
- One To One Relations
- One To Many Relations
- Many To Many Relations
- Has Many Through Relations
- One To One Polymorphic Relations
- One To Many Polymorphic Relations
- Many To Many Polymorphic Relations
Observers
Starting from v0.4 it is possible to use relationship events in Laravel observer classes. Add HasRelationshipObservables trait to your model and define an observer:
namespace App\Observer; class UserObserver { public function hasOneCreating(User $user, Model $related) { Log::info("Creating profile for user {$related->name}."); } public function hasOneCreated(User $user, Model $related) { Log::info("Profile for user {$related->name} has been created."); } }
Register the observer in AppServiceProvider:
public function boot() { User::observe(UserObserver::class); }
Credits
- Original package by @chelout — chelout/laravel-relationship-events
- Maintained by @sameedkun
License
MIT