turahe/laravel-userstamps

一个用于向模型添加 created_by, updated_by 和 deleted_by 字段的包。

v1.0.1 2024-09-23 10:12 UTC

This package is auto-updated.

Last update: 2024-09-23 10:15:41 UTC


README

提供了一个 Eloquent 特性,可以自动在您的模型上维护 created_by、updated_by 和 deleted_by(当使用 softDeletes 时)字段,通过当前登录的用户。

安装和用法

此包需要 PHP 8 和 Laravel 5.6 或更高版本。在您的控制台中运行以下命令来安装包:

composer require turahe/laravel-userstamps

您可以使用以下命令发布配置文件:

php artisan vendor:publish --provider="Turahe\UserStamps\UserStampsServiceProvider" --tag="config"

这是发布配置文件的内容

return [

    /*
     * Define the table which is used in the database to retrieve the users
     */

    'users_table' => 'users',
    
    /*
     * Define the table column type which is used in the table schema for
     * the id of the user
     *
     * Options: increments, bigIncrements, uuid
     * Default: bigIncrements
     */

    'users_table_column_type' => 'bigIncrements',

    /*
     * Define the name of the column which is used in the foreign key reference
     * to the id of the user
     */

    'users_table_column_id_name' => 'id',
    
    /*
     * Define the mmodel which is used for the relationships on your models
     */
    
    'users_model' => \App\Models\User::class,
    
    /*
     * Define the column which is used in the database to save the user's id
     * which created the model.
     */

    'created_by_column' => 'created_by',

    /*
     * Define the column which is used in the database to save the user's id
     * which updated the model.
     */

    'updated_by_column' => 'updated_by',

    /*
     * Define the column which is used in the database to save the user's id
     * which deleted the model.
     */

    'deleted_by_column' => 'deleted_by',

];

将宏添加到您的模型迁移中

public function up()
{
    Schema::create('table_name', function (Blueprint $table) {
        ...

        $table->userstamps();
        $table->softUserstamps();
    });
}   

将特性添加到您的模型中

use Turahe\UserStamps\Concerns\HasUserStamps;

class Example extends Model {

    use HasUserStamps;
}

将提供方法来检索执行创建、更新或删除操作的用户对象。

$model->author; // the user who created the model
$model->editor; // the user who last updated the model
$model->destroyer; // the user who deleted the model