snappshop/actor

v1.2.2 2023-07-03 16:10 UTC

This package is auto-updated.

Last update: 2024-09-03 18:54:57 UTC


README

品牌选项,可在模型中自动生成特定动作字段。使用此包可以帮助您在团队中通用字段。

安装

为了将此功能添加到您的laravel应用程序中,您应该通过composer要求它。

composer require snappshop/actor

发布配置文件

通过发布配置文件,可以编辑预定义的自定义快捷宏。

php artisan vendor:publish --provider="SnappShop\Actor\Providers\ActorServiceProvider" --tag="actor-config"

用法

基本用法

您可以将actor用作迁移中的宏。动作的动词形式作为第一个参数传递。

Schema::table('users', function (Blueprint $table) {
    ...
    $table->actor('test');
    ...
});

在提供的示例中,$table->actor('test');将创建字段tester_idtester_type

快捷宏

您可以使用一些宏作为快捷方式。默认情况下,定义了creatoreditor宏。

Schema::table('users', function (Blueprint $table) {
    ...
    $table->creator();
    $table->editor();
    ...
});

在提供的示例中,为用户表创建了字段creator_idcreator_typeeditor_ideditor_type

定义自定义快捷宏

使用提供的配置文件添加或删除自定义快捷宏。

return [
    'custom-macros' => [
        'edit',
        'approve',
    ]
];

在提供的示例中,移除了create宏,并添加了approve宏到代码中;

模型特质

该包提供了一个特质:Actorable。定义了actorable函数来处理自动设置功能。

use \Snappshop\Actor\Traits\Actorable;

class User
{
    use Actorable;
    
    public function actorable(): array
    {
        return [
            'actions' => [
                'edit'
            ]
        ];
    } 
}

在上面的示例中,与edit相关的字段在模型更新时自动设置。目前,创建动作也可以自动设置。

检索动作数据

虽然您可以直接通过字段名访问字段,但有一些函数可以访问动作数据。

可用方法

以下方法是通过在模型上使用Actorable特质添加的。很明显,在迁移中应该先使用宏来生成相关列。

获取动作ID

检索给定动作的动作ID。

getActorId(string $action): int|string;

//  example output: 13

获取动作类型

检索给定动作的动作类型。

getActorType(string $action): ?string;

//  example output: "\App\Models\User"

获取动作时间

检索给定动作的动作时间。

getActedAt(string $action): ?Carbon;

//  example output: "2023-04-30 15:30:04"

获取动作者

检索动作者模型。

getActor(string $action): ?Model;

获取动作

返回一个包含所有上述数据的数组。

getAct(string $action): array;

//  example output: [
//    'editor_id' => 13,
//    'editor_type' => "\App\Models\User",
//    'edited_at' => "2023-04-30 15:30:04",
//  ]

设置动作数据

动作字段可以用以下方法设置。

触摸动作

此方法提供了一种使用认证用户同时设置所有动作字段的方法。

touchAction(string $action, bool $isForce = false): void;

清除动作

此方法提供了一种同时清除所有动作字段的方法。

cleanAction(string $action): void;

检查和过滤动作数据

这些附件用于检查和过滤动作数据

检查是否由特定用户执行

此方法用于检查模型是否在特定动作上由特定用户执行

isActedBy(string $action, ?Authenticatable $user): bool;

如果模型在动作上由给定用户执行,则返回true

动作者范围

提供了一个范围用于过滤动作者模型数据以获取给定动作上的给定动作的记录。

$query
...
    ->actedBy('create', $user)
...

上述范围表达式使用给定的动作和认证用户过滤了动作者模型查询。

许可证

Laravel Actor包是开源软件,使用MIT许可证。