mikebronner/laravel-pivot-events
Adds Eloquent model events for many-to-many pivot operations: sync, attach, detach, and updateExistingPivot on BelongsToMany/MorphToMany. Listen for pivotSyncing/Synced, pivotAttaching/Attached, pivotDetaching/Detached, and pivotUpdating/Updated.
Architecture Fit
created, updated). It introduces pivot-specific events (pivotSynced, pivotAttached, etc.), which are logically scoped to BelongsToMany/MorphToMany operations, reducing noise in global event listeners.sync(), attach(), etc.) via traits, ensuring performance parity with vanilla Laravel.GeneaLabs/laravel-model-caching) without conflicts.hasManyThrough or custom pivot models (only standard pivot tables).User::roles()->sync() triggers events on the User model, not the relationship itself).Technical Risk
PivotEventTrait is opt-in per model, reducing blast radius.pivotSynced, race conditions could occur if listeners modify the same pivot table. Mitigation: Use transactions or queue jobs.intermediateTable with custom pivot attributes, ensure the package’s default payload structure ($pivotIds, $pivotIdsAttributes) aligns with your schema.BelongsToMany; validate with your specific morph map setup.Key Questions for the Team
Stack Fit
Illuminate/Support for events).laravel/telescope and GeneaLabs/laravel-model-caching (tested in the package’s roadmap).Migration Path
BelongsToMany/MorphToMany relationships to identify high-value targets for event-driven logic.DB::table() calls vs. Eloquent methods).PivotEventTrait to a single model (e.g., User with roles() relationship).pivotAttached/pivotDetached to validate payload structure and performance.User, Product) with rollback plans for event-related bugs.Compatibility
sync(), attach(), etc.).eloquent.pivotAttached).pivotAttached → SendNotificationJob).Sequencing
laravel/framework to the latest stable version.App\Models\Model) or apply selectively.EventServiceProvider.$pivotIds and $pivotIdsAttributes).pivotSynced firing 10x more than usual).Maintenance
$changes in pivotSynced (e.g., ["attached" => [1, 2], "detached" => [3]]) may require documentation.Support
static::pivotAttached(fn(...)) in boot().eloquent.pivotAttached vs. ModelPivotAttached).$pivotIdsAttributes vs. $changes).syncWithoutEvents for critical operations where events must not fire.Scaling
How can I help you explore Laravel packages today?