Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

laravel-pivot

> 后端框架
Open source

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

524 stars0 likes4 views
WebsiteGitHub

About

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

Laravel Pivot

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

Laravel Problems

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

Version Compatibility

Laravel Version Package Tag Supported Development Branch
>= 5.5.0 3.* yes master
belongsToMany(Role::class);
}

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);
});

```php
class Role extends Model
{
    ....

Attaching

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

Attaching with int

Running this code

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

You will see this output

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

Attaching with array

Running this code

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

You will see this output

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

Attaching with model

Running this code

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

You will see this output

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

Attaching with collection

Running this code

php
$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 => attributes)

Running this code

php
$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:

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

Running this code

php
$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:

Running this code

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

You will see this output

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

Updating:

Running this code

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

You will see this output

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

License

MIT

Free Software, Hell Yeah!

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPattach-eventsbelongstomany-eventseloquent-eventslaravel

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架