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.
boot(). No database migrations or schema changes are needed.hasManyUpdated). Risk of N+1 queries if not optimized (e.g., eager loading).with() clauses, caching).v5.x for Laravel 13-specific changes.^5.0) and test upgrades.hasManySaved event might trigger a queue job). Requires integration tests to verify event flows.tntsearch/laravel-query-cache if needed.hasManyCreating vs. hasManySaved) or coarser-grained (e.g., aggregated RelationshipUpdated)?HasOneSaved::class) necessary, or are closures sufficient? Dispatchable events add complexity but enable queueing/retry logic.hasManySaved).creating, created, etc.—no relationship-specific events.save(), attach(), associate()) to identify where events add value.Post::tags()->sync() triggers side effects (e.g., updating a search index), this package eliminates manual hooks.User::posts()) and implement HasManyEvents.afterSave hooks) with package events.save() or create() (e.g., spatie/laravel-activitylog) may interfere.composer require chelout/laravel-relationship-events
php artisan vendor:publish if extending default config (none exists yet).use HasManyEvents on Post for tags()).boot() after parent::boot().hasManySaved fires on save()).public function test_has_many_saved_event()
{
$post = Post::factory()->create();
$tag = Tag::factory()->create();
$post->tags()->save($tag);
$this->assertLogged('Tags have been attached to post '.$post->title);
}
v5.0.0 → v5.1.0).v4.x → v5.x) may require Laravel upgrades.hasOneUpdated) add technical debt. Document event ownership (e.g., "Team A owns OrderItem::hasManySaved").dd() or Log::debug() in event handlers to trace execution.hasManyUpdated fetch related collections—ensure foreign keys are set.morphToManyAttached in a tagging system) may overload queues.
belongsToAssociated fetches parent). Optimize with:
sync()) in database transactions.How can I help you explore Laravel packages today?