百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

laravel-pivot

> 后端框架
开源

此包引入了 BelongsToMany 关系上的 sync()、attach()、detach() 或 updateExistingPivot() 方法的新事件。

524 stars0 点赞4 次浏览
访问官网GitHub

工具介绍

此包引入了 BelongsToMany 关系上的 sync()、attach()、detach() 或 updateExistingPivot() 方法的新事件。

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 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPattach-eventsbelongstomany-eventseloquent-eventslaravel

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

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