nicolassfr/laravel-pivot

此包已被废弃,不再维护。未建议替代包。

fico7489/laravel-pivot 的自定义包,为 self fire sync() Laravel 方法添加 pivotSyncing 和 pivotSynced 方法。

dev-master 2019-10-28 12:32 UTC

This package is auto-updated.

Last update: 2021-01-28 15:50:41 UTC


README

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

Laravel 问题

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

版本兼容性

Laravel 版本 包标签 支持 开发分支
>= 5.5.0 3.* yes master
< 5.5.0 - no -
  • 您仍然可以使用 inactive branches 为 laravel 5.4.x 或更早版本使用

安装

1. 使用 composer 安装包

composer require nicolassfr/laravel-pivot

此语句将安装适用于您当前 Laravel 版本的最高可用包版本。

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

...
use Nicolassfr\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)

新事件是

pivotSyncing, pivotSynced,
pivotAttaching, pivotAttached,
pivotDetaching, pivotDetached,
pivotUpdating, pivotUpdated

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

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

    static::pivotSyncing(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
        //
    });
    
    static::pivotSynced(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
        //
    });
    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()
触发一个 pivotAttaching 和一个 pivotAttached 事件。
即使添加了更多行,也只会为所有行触发一个事件,但在这种情况下,您可以在 $pivotIds 变量中看到所有更改的行 ID,并在 $pivotIdsAttributes 变量中看到具有属性的更改行 ID。

detach()
触发一个 pivotDetaching 和一个 pivotDetached 事件。
即使删除了更多行,也只会为所有行触发一个事件,但在这种情况下,您可以在 $pivotIds 变量中看到所有更改的行 ID。

updateExistingPivot()
触发一个 pivotUpdating 和一个 pivotUpdated 事件。
您可以使用 updateExistingPivot 仅更改关联表中的一行。

sync()
根据关联表中添加的行数触发多个 pivotAttachingpivotAttached 事件。如果没有添加任何内容,则不会触发这些事件。
触发一个 pivotDetaching 和一个 pivotDetached 事件,但您可以在 $pivotIds 变量中看到所有被删除的 ID。如果没有元素被移除,则不会触发此事件。
例如,当您调用 sync() 方法时,如果添加了两行并删除了两行,将会触发 两个 pivotAttaching两个 pivotAttached 事件,以及一个 pivotDetaching 和一个 pivotDetached 事件。
如果调用 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

自由软件,太棒了!

感谢 fico7489 提供初始包