PHPackages                             cheesegrits/laravel-pivot-events - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. cheesegrits/laravel-pivot-events

ActiveLibrary[Database &amp; ORM](/categories/database)

cheesegrits/laravel-pivot-events
================================

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

v1.0(2mo ago)137MITPHP

Since May 5Pushed 2mo agoCompare

[ Source](https://github.com/cheesegrits/laravel-pivot-events)[ Packagist](https://packagist.org/packages/cheesegrits/laravel-pivot-events)[ Docs](https://github.com/cheesegrits/laravel-pivot-events)[ RSS](/packages/cheesegrits-laravel-pivot-events/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

Laravel Pivot Events
====================

[](#laravel-pivot-events)

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

This package is based on the [laravel-pivot](https://github.com/fico7489/laravel-pivot) package by Filip Horvat. The original package is abandoned and does not work with Laravel 13.

Laravel Problems
----------------

[](#laravel-problems)

In Laravel events are not dispatched when a BelongsToMany relation (pivot table) is updated with sync(), attach(), detach() or updateExistingPivot() methods, but this package will help with that.

Version Compatibility
---------------------

[](#version-compatibility)

Compatible with Laravel 11.0 or higher.

Install
-------

[](#install)

1.Install package with composer

```
composer require cheesegrits/laravel-pivot-events

```

With this statement, a composer will install highest available package version for your current laravel version.

2.Use Cheesegrits\\Laravel\\PivotEvents\\Traits\\PivotEventTrait trait in your base model or only in particular models.

```
use Cheesegrits\Laravel\PivotEvents\Traits\PivotEventTrait;
use Illuminate\Database\Eloquent\Model;

abstract class BaseModel extends Model
{
    use PivotEventTrait;
...
```

and that's it, enjoy.

New eloquent events
-------------------

[](#new-eloquent-events)

New events are :

```
pivotAttaching, pivotAttached
pivotDetaching, pivotDetached,
pivotUpdating, pivotUpdated

```

The best way to catch events is with a model observer:

```
use App\Models\YourModel;

class YourModelObserver
{
    public function saving(User $user)
    {
        //this is how we catch pivot events
    }

    public function pivotAttaching(function (YourModel $model, string $relationName, array $pivotIds, array $pivotIdsAttributes) {
        //
    });

    public function pivotAttached(function (YourModel $model, string $relationName, string $pivotIds, string $pivotIdsAttributes) {
        //
    });

    public function pivotDetaching(function (YourModel $model, string $relationName, string $pivotIds) {
        //
    });

    public function pivotDetached(function (YourModel $model, string $relationName, string $pivotIds) {
        //
    });

    public function pivotUpdating(function (YourModel $model, string $relationName, string $pivotIds, string $pivotIdsAttributes) {
        //
    });

    public function pivotUpdated(function (YourModel $model, string $relationName, string $pivotIds, string $pivotIdsAttributes) {
        //
    });

    public function updating(function (YourModel $model) {
        //this is how we catch standard eloquent events
    });
}
```

Or with a boot method:

```
use Cheesegrits\Laravel\PivotEvents\Traits\PivotEventTrait;
use Illuminate\Database\Eloquent\Model;

class YourModel extends Model
{
    use PivotEventTrait;

    public static function boot()
    {
        parent::boot();

        static::pivotAttaching(function (YourModel $model, string $relationName, array $pivotIds, array $pivotIdsAttributes) {
            //
        });

        // etc.
    }
}
```

You can also see those events here:

```
\Event::listen('eloquent.*', function ($eventName, array $data) {
    echo $eventName;  //e.g. 'eloquent.pivotAttached'
});
```

Supported relations
-------------------

[](#supported-relations)

**BelongsToMany** and **MorphToMany**

Which events are dispatched and when they are dispatched
--------------------------------------------------------

[](#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 **more** **pivotAttaching** and **more** **pivotAttached** events, depending on how many rows are added in the pivot table. These events are not dispatched if nothing is attached.
Dispatches **one** **pivotDetaching** and **one** **pivotDetached** event, but you can see all deleted ids in the $pivotIds variable. This event is not dispatched if nothing is detached.
Dispatches **more** **pivotUpdating** and **more** **pivotUpdated** events, depending on how many rows are updated in the pivot table. These events are not dispatched if nothing is attached.

E.g. when you call sync() if two rows are added and two are deleted **two** **pivotAttaching** and **two** **pivotAttached** events and **one** **pivotDetaching** and **one** **pivotDetached** event will be dispatched.
If sync() is called but rows are not added or deleted events are not dispatched.

Usage
-----

[](#usage)

We have three tables in database users(id, name), roles(id, name), role\_user(user\_id, role\_id). We have two models :

```
class User extends Model
{
    use PivotEventTrait;
    // ...

    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }

    public static function boot()
    {
        parent::boot();

        static::pivotAttached(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
            echo 'pivotAttached';
            echo get_class($model);
            echo $relationName;
            print_r($pivotIds);
            print_r($pivotIdsAttributes);
        });

        static::pivotUpdated(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
            echo 'pivotUpdated';
            echo get_class($model);
            echo $relationName;
            print_r($pivotIds);
            print_r($pivotIdsAttributes);
        });

        static::pivotDetached(function ($model, $relationName, $pivotIds) {
            echo 'pivotDetached';
            echo get_class($model);
            echo $relationName;
            print_r($pivotIds);
        });
    }
```

```
class Role extends Model
{
    // ...
}
```

### Attaching

[](#attaching)

For attach() or detach() one event is dispatched for both pivot ids.

#### Attaching with int

[](#attaching-with-int)

Running this code

```
$user = User::first();
$user->roles()->attach(1);
```

You will see this output

```
pivotAttached
App\Models\User
roles
[1]
[1 => []]

```

#### Attaching with array

[](#attaching-with-array)

Running this code

```
$user = User::first();
$user->roles()->attach([1]);
```

You will see this output

```
pivotAttached
App\Models\User
roles
[1]
[1 => []]

```

#### Attaching with model

[](#attaching-with-model)

Running this code

```
$user = User::first();
$user->roles()->attach(Role::first());
```

You will see this output

```
pivotAttached
App\Models\User
roles
[1]
[1 => []]

```

#### Attaching with collection

[](#attaching-with-collection)

Running this code

```
$user = User::first();
$user->roles()->attach(Role::get());
```

You will see this output

```
pivotAttached
App\Models\User
roles
[1, 2]
[1 => [], 2 => []]

```

#### Attaching with array (id =&gt; attributes)

[](#attaching-with-array-id--attributes)

Running this code

```
$user = User::first();
$user->roles()->attach([1, 2 => ['attribute' => 'test']], ['attribute2' => 'test2']);
```

You will see this output

```
pivotAttached
App\Models\User
roles
[1, 2]
[1 => [], 2 => ['attribute' => 'test', 'attribute2' => 'test2']]

```

### Syncing:

[](#syncing)

For sync() method event is dispatched for each pivot row.

Running this code

```
$user = User::first();
$user->roles()->sync([1, 2]);
```

You will see this output

```
pivotAttached
App\Models\User
roles
[1]
[1 => []]

pivotAttached
App\Models\User
roles
[2]
[2 => []]

```

### Detaching:

[](#detaching)

Running this code

```
$user = User::first();
$user->roles()->detach([1, 2]);
```

You will see this output

```
pivotDetached
App\Models\User
roles
[1, 2]

```

### Updating:

[](#updating)

Running this code

```
$user = User::first();
$user->roles()->updateExistingPivot(1, ['attribute' => 'test']);
```

You will see this output

```
pivotUpdated
App\Models\User
roles
[1]
[1 => ['attribute' => 'test']]

```

License
-------

[](#license)

MIT

**Free Software, Hell Yeah!**

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance84

Actively maintained with recent releases

Popularity12

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity34

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

80d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/826fa86e4930c9a52697c428cee6ef0f32d86f5416af9190aba906af27579ae8?d=identicon)[cheesegrits](/maintainers/cheesegrits)

---

Top Contributors

[![cheesegrits](https://avatars.githubusercontent.com/u/934456?v=4)](https://github.com/cheesegrits "cheesegrits (2 commits)")

---

Tags

laravel pivot eventslaravel BelongsToMany eventseloquent extra eventslaravel sync eventseloquent events

###  Code Quality

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/cheesegrits-laravel-pivot-events/health.svg)

```
[![Health](https://phpackages.com/badges/cheesegrits-laravel-pivot-events/health.svg)](https://phpackages.com/packages/cheesegrits-laravel-pivot-events)
```

###  Alternatives

[fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

5246.4M24](/packages/fico7489-laravel-pivot)[genealabs/laravel-pivot-events

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

1425.2M9](/packages/genealabs-laravel-pivot-events)[mikebronner/laravel-pivot-events

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

141768.5k17](/packages/mikebronner-laravel-pivot-events)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M98](/packages/mongodb-laravel-mongodb)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[bavix/laravel-wallet

It's easy to work with a virtual wallet.

1.3k1.3M19](/packages/bavix-laravel-wallet)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
