richan-fongdasen/eloquent-blameable

为您的 Laravel Eloquent 模型实现可归责行为

1.10.0 2024-03-13 10:02 UTC

This package is auto-updated.

Last update: 2024-09-13 10:57:00 UTC


README

CI codecov Total Downloads Latest Stable Version License: MIT

Eloquent Blameable

为您的 Laravel Eloquent 模型实现可归责行为

摘要

此包可以帮助您跟踪每个数据库记录的创建者和更新者。它将自动填充指定的属性以当前用户 ID。默认情况下,这些属性会在您保存 Eloquent 模型对象时填充。

目录

设置

使用 Composer 安装包

$ composer require richan-fongdasen/eloquent-blameable

Laravel 版本兼容性

如果您使用的是 Laravel 版本 5.5+,则可以跳过在您的 Laravel 应用程序中注册服务提供者。

服务提供者

在您的 config/app.php 中添加包服务提供者

'providers' => [
    // ...
    RichanFongdasen\EloquentBlameable\ServiceProvider::class,
];

配置

使用 php artisan 命令发布配置文件

$ php artisan vendor:publish --provider="RichanFongdasen\EloquentBlameable\ServiceProvider"

上述命令将一个新的配置文件复制到 /config/blameable.php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Guard
    |--------------------------------------------------------------------------
    |
    | Please specify your default authentication guard to be used by blameable
    | service. You can leave this to null if you're using the default Laravel
    | authentication guard.
    |
    | You can also override this value in model classes to use a different
    | authentication guard for your specific models.
    | IE: Some of your models can only be created / updated by specific users
    | who logged in from a specific authentication guard.
    |
    */

    'guard' => null,

    /*
    |--------------------------------------------------------------------------
    | User Model Definition
    |--------------------------------------------------------------------------
    |
    | Please specify a user model that should be used to setup `creator`
    | and `updater` relationship.
    |
    */

    'user' => \App\User::class,

    /*
    |--------------------------------------------------------------------------
    | The `createdBy` attribute
    |--------------------------------------------------------------------------
    |
    | Please define an attribute to use when recording the creator
    | identifier.
    |
    */

    'createdBy' => 'created_by',

    /*
    |--------------------------------------------------------------------------
    | The `updatedBy` attribute
    |--------------------------------------------------------------------------
    |
    | Please define an attribute to use when recording the updater
    | identifier.
    |
    */

    'updatedBy' => 'updated_by',

    /*
    |--------------------------------------------------------------------------
    | The `deletedBy` attribute
    |--------------------------------------------------------------------------
    |
    | Please define an attribute to use when recording the user
    | identifier who deleted the record. This feature would only work
    | if you are using SoftDeletes in your model.
    |
    */

    'deletedBy' => 'deleted_by',
];

使用

在迁移中添加一些可归责的属性

Schema::create('some_tables', function (Blueprint $table) {
    // ...

    $table->integer('created_by')->nullable();
    $table->integer('updated_by')->nullable();
    $table->integer('deleted_by')->nullable();

    // ...



    /**
     * You can also create foreign key constrains
     * for the blameable attributes.
     */
    $table->foreign('created_by')
        ->references('id')->on('users')
        ->onDelete('cascade');

    $table->foreign('updated_by')
        ->references('id')->on('users')
        ->onDelete('cascade');
});

将可归责行为附加到您的模型中

use Illuminate\Database\Eloquent\Model;
use RichanFongdasen\EloquentBlameable\BlameableTrait;

class Post extends Model
{
    use BlameableTrait;

    // ...
}

使用静态属性覆盖默认配置

    /**
     * You can override the default configuration
     * by defining this static property in your Model
     */
    protected static $blameable = [
        'guard' => 'customGuard',
        'user' => \App\User::class,
        'createdBy' => 'user_id',
        'updatedBy' => null
    ];

使用公共方法覆盖默认配置

    /**
     * You can override the default configuration
     * by defining this method in your Model
     */
    public function blameable()
    {
        return [
            'guard' => 'customGuard',
            'user' => \App\User::class,
            'createdBy' => 'user_id',
            'updatedBy' => null
        ];
    }

使用可归责查询作用域

// Get all posts which have created by the given user id
Post::createdBy($userId)->get();

// Get all posts which have updated by the given user object
$user = User::findOrFail(1);
Post::updatedBy($user)->get();

访问创建者/更新者对象

// Get the creator user object
Post::findOrFail($postId)->creator;

// Get the updater user object
Post::findOrFail($postId)->updater;

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件