istanbay/laravel-pivot

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

3.1 2019-12-16 14:06 UTC

README

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

Laravel问题

在Laravel中,当使用sync()、attach()、detach()或updateExistingPivot()方法更新BelongsToMany关系(关联表)时,不会触发事件,但此包可以帮助解决这个问题。

版本兼容性

  • 您仍然可以使用laravel 5.4.x或更早版本的无效分支

安装

1. 使用composer安装包

composer require fico7489/laravel-pivot

此声明将安装当前Laravel版本可用的最高版本包。

2. 在您的基模型或特定模型中使用Fico7489\Laravel\Pivot\Traits\PivotEventTrait特性。

...
use Fico7489\Laravel\Pivot\Traits\PivotEventTrait;
use Illuminate\Database\Eloquent\Model;

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

这就完成了,享受吧。

新的Eloquent事件

您可以在这里查看所有Eloquent事件:https://laravel.net.cn/docs/5.5/eloquent#events)

新事件包括

pivotAttaching, pivotAttached
pivotDetaching, pivotDetached,
pivotUpdating, pivotUpdated

捕获事件的最佳方式是使用这些模型函数

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

    static::pivotAttaching(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
        //
    });
    
    static::pivotAttached(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
        //
    });
    
    static::pivotDetaching(function ($model, $relationName, $pivotIds) {
        //
    });

    static::pivotDetached(function ($model, $relationName, $pivotIds) {
        //
    });
    
    static::pivotUpdating(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
        //
    });
    
    static::pivotUpdated(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
        //
    });
    
    static::updating(function ($model) {
        //this is how we catch standard eloquent events
    });
}

您还可以在这里看到这些事件

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

支持的关联

BelongsToManyMorphToMany

触发事件的时间和条件

四个BelongsToMany方法从这个包中触发事件

attach()
触发一个事件。
即使添加了更多行,也只为所有行触发一个事件,但您可以在变量中看到所有更改的行ID,以及在变量中更改的行ID及其属性。

detach()
触发一个和一个事件。
即使删除了更多行,也只为所有行触发一个事件,但您可以在变量中看到所有更改的行ID。

updateExistingPivot()
触发一个和一个事件。
您只能使用updateExistingPivot更改关联表中的一行。

sync()
根据在关联表中添加的行数,触发多个事件。如果没有附加任何内容,则不会触发这些事件。
触发一个和一个事件,但您可以在变量中看到所有已删除的ID。如果没有删除任何内容,则不会触发此事件。
例如,当调用sync()时,如果添加了两行并删除了两行,将触发两个和两个事件,以及一个和一个事件。
如果调用sync()但没有添加或删除行,则不会触发事件。

用法

数据库中有三个表:users(id, name),roles(id, name),role_user(user_id, role_id)。我们有两个模型

...
class User extends Model
{
    use PivotEventTrait;
    ....
    
    public function roles()
    {
        return $this->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);
    });
...
class Role extends Model
{
    ....

附加

对于attach()或detach(),为每个pivot id触发一个事件。

使用整数附加

运行此代码

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

您将看到以下输出

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

使用数组附加

运行此代码

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

您将看到以下输出

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

使用模型附加

运行此代码

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

您将看到以下输出

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

使用集合附加

运行此代码

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

您将看到以下输出

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

使用数组(id => attributes)附加

运行此代码

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

您将看到以下输出

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

同步

对于sync()方法,为每个pivot行触发一个事件。

运行此代码

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

您将看到以下输出

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

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

分离

运行此代码

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

您将看到以下输出

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

更新

运行此代码

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

您将看到以下输出

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

许可

MIT

自由软件,太棒了!