signifly/laravel-pivot-events

在附加、分离或更新Eloquent模型关联时触发模型事件。

v2.0.1 2023-11-21 14:09 UTC

This package is auto-updated.

Last update: 2024-09-21 16:03:34 UTC


README

Latest Version on Packagist Build Status StyleCI Quality Score Total Downloads

signifly/laravel-pivot-events 包允许您轻松地将Eloquent模型关联事件添加到您的Laravel应用程序中。

以下是使用示例。

// Remember to add use statement
use Signifly\PivotEvents\HasPivotEvents;

class User
{
    use HasPivotEvents;
}

现在您将能够监听新可用的关联事件

use Signifly\PivotEvents\HasPivotEvents;

class User
{
    use HasPivotEvents;

    protected static function boot()
    {
        static::pivotAttaching(function ($model) {
            // To get related changes
            $model->getPivotChanges();
            // return Collection(['attach' => ['roles' => [1 => ['scopes' => 'orders']]]])

            // To get related changes for a specific type
            $model->getPivotChanges('attach');
            // return Collection(['roles' => [1 => ['scopes' => 'orders']]])
            
            // You can get nested changes
            // values are $id => $attributes
            $model->getPivotChanges('attach.roles');
            // return Collection([1 => ['scopes' => 'orders']])

            // To get related ids for a specific type and relation
            $model->getPivotChangeIds('attach', 'roles');
            // return Collection([1])
        });

        static::pivotAttached(function ($model) {
            //
        });

        static::pivotDetaching(function ($model) {
            //
        });

        static::pivotDetached(function ($model) {
            //
        });

        static::pivotUpdating(function ($model) {
            //
        });

        static::pivotUpdated(function ($model) {
            //
        });
    }
}

您还可以使用观察者来处理以下事件:`pivotAttaching`、`pivotAttached`、`pivotDetaching`、`pivotDetached`、`pivotUpdating`、`pivotUpdated`。

文档

在提供更多文档之前,请查看测试用例。

安装

您可以通过composer安装此包

$ composer require signifly/laravel-pivot-events

包将自动注册自身。

测试

$ composer test

安全性

如果您发现任何安全问题,请发送电子邮件到 dev@signifly.com 而不是使用问题跟踪器。

致谢

许可证

MIT许可证(MIT)。请参阅 许可证文件 以获取更多信息。