Download the PHP package genealabs/laravel-pivot-events without Composer
On this page you can find all versions of the php package genealabs/laravel-pivot-events. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download genealabs/laravel-pivot-events
More information about genealabs/laravel-pivot-events
Files in genealabs/laravel-pivot-events
Package laravel-pivot-events
Short Description This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.
License MIT
Homepage https://github.com/mikebronner/laravel-pivot-events
Informations about the package laravel-pivot-events
Laravel Pivot Events
This package introduces new eloquent events for sync(), attach(), detach(), or updateExistingPivot() methods on BelongsToMany and MorphToMany relationships.
This package is a fork of fico7489/laravel-pivot created mainly to address compatibility issues with Laravel Telescope and Model Caching for Laravel.
Sponsors
We thank the following sponsors for their generosity. Please take a moment to check them out:
Requirements
- Laravel 8.0+
- PHP 7.3+
Installation
1.Install package with composer:
- Use
GeneaLabs\LaravelPivotEvents\Traits\PivotEventTrait
trait in your base model or only in particular models.
New Eloquent Events
You can check all eloquent events here: https://laravel.com/docs/5.8/eloquent#events)
New events are :
pivotSyncing
,pivotSynced
pivotAttaching
,pivotAttached
pivotDetaching
,pivotDetached
pivotUpdating
,pivotUpdated
The easiest way to catch events is using methods in your model's boot()
method:
You can also catch them using dedicated Event Listeners:
Supported Relationships
BelongsToMany and MorphToMany
Which events are dispatched and when they are dispatched
Four BelongsToMany methods dispatches events from this package :
attach()
Dispatches one pivotAttaching and one pivotAttached event.
Even when more rows are added only one event is dispatched for all rows but in that case, you can see all changed row ids in the $pivotIds variable, and the changed row ids with attributes in the $pivotIdsAttributes variable.
detach()
Dispatches one pivotDetaching and one pivotDetached event.
Even when more rows are deleted only one event is dispatched for all rows but in that case, you can see all changed row ids in the $pivotIds variable.
updateExistingPivot()
Dispatches one pivotUpdating and one pivotUpdated event.
You can change only one row in the pivot table with updateExistingPivot.
sync()
Dispatches one pivotSyncing and one pivotSynced event.
Whether a row was attached/detached/updated during sync only one event is dispatched for all rows but in that case, you can see all the attached/detached/updated rows in the $changes variables.
E.g. How does sync work: The sync first detaches all associations and then attaches or updates new entries one by one.
Usage
We have three tables in database users(id, name), roles(id, name), role_user(user_id, role_id). We have two models :
Attaching
For attach() or detach() one event is dispatched for both pivot ids.
Attaching With Primary Key
Running this code
You will see this output
Attaching with array
Running this code
You will see this output
Attaching with model
Running this code
You will see this output
Attaching with collection
Running this code
You will see this output
Attaching with array (id => attributes)
Running this code
You will see this output
Syncing
Running this code
You will see this output
Detaching
Running this code
You will see this output
Updating
Running this code
You will see this output
All versions of laravel-pivot-events with dependencies
illuminate/support Version ^8.0|^9.0|^10.0|^11.0