kamansoft / laravel-blame
一个用于轻松处理laravel eloquent模型中updated_by和created_by字段的工具
Requires
- php: ^8.1
- doctrine/dbal: ^3.5
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-08 04:13:52 UTC
README
在您的laravel模型中更新_by和created_by字段?
这是一个laravel包,它将简化通常称为created_by和update_by的额外字段的用法,这些字段用于在记录持久化创建或更新事件时建立责任,类似于Eloquent模型上的“时间戳字段”。它将自动使用当前登录用户的主键或预配置的系统用户ID填充这些字段。
要求
此包是在假设您使用laravel与eloquent用户模型类作为认证提供者的情况下构建的。因此,在安装之前,您必须确保您的用户eloquent模型已设置在auth配置文件的提供者部分,如下所示:
//config/auth.php 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, // <-- this is the important part ], ],
安装
您可以通过composer安装此包
composer require kamansoft/laravel-blame
有时,您的laravel应用程序将执行操作,在数据库中持久化记录,而没有经过身份验证或登录的用户。您可以将此用户称为系统用户或任何您想要的名称,但您必须指定其ID或主键。
为此,您可以运行系统用户命令或包安装命令
php artisan blame:install
上述命令将发布包配置文件,并运行不带参数的系统用户命令。
用法
您必须首先将update_by和created_by字段添加到表的迁移字段中,以便在模型中使用它。
//dabase/migrations/2023_01_02_154102_create_somes_table use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up() { Schema::create('somes', function (Blueprint $table) { $table->uuid('id')->primary(); $table->string('name'); $system_user_id = env('BLAME_SYSTEM_USER_ID'); $table->unsignedBigInteger('created_by')->default($system_user_id); $table->unsignedBigInteger('updated_by')->default($system_user_id); $table->foreign('created_by')->references('id')->on('users'); $table->foreign('updated_by')->references('id')->on('users'); $table->timestamps(); }); }
然后只需在您的eloquent模型上使用Modelblamer特性,它的启动方法将负责其余部分。此外,为了良好的实践,让您的模型在您的模型上实现BlameableInterface。然后,您的模型应类似于以下内容:
//app/Models/SomeModel.php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Kamansoft\LaravelBlame\Contracts\ModelBlame; use Kamansoft\LaravelBlame\Traits\ModelBlamer; class SomeModel extends Model implements ModelBlame { use ModelBlamer; // <-- this is the important part
命令
此包提供了两个artisan命令作为工具,以简化包的使用,简化重复的任务,例如创建或更新系统用户或将updated_by和created_by字段添加到模型的表中。
Blame Fields Migration命令
此命令主要用于在需要更新现有表,添加created_by和updated_by字段时使用,它需要一个参数,即要更新的表的名称。
php artisan blame:make:migration some_table_name
运行上述命令时,将在数据库/migrations文件夹中创建一个新的迁移文件,文件名为add_blame_fields_to_some_table_name_table,其内容类似于以下内容:
//database/migrations/2023_01_02_154102_add_blame_fields_to_some_table_name_table.php return new class extends Migration { public function up() { Schema::table('some_table_name', function (Blueprint $table) { $system_user_id = env('BLAME_SYSTEM_USER_ID'); $table->unsignedBigInteger(config('blame.created_by_field_name'))->default($system_user_id); $table->unsignedBigInteger(config('blame.updated_by_field_name'))->default($system_user_id); }); Schema::table('some_table_name', function (Blueprint $table) { $table->unsignedBigInteger(config('blame.created_by_field_name'))->default(null)->change(); $table->unsignedBigInteger(config('blame.updated_by_field_name'))->default(null)->change(); }); Schema::table('some_table_name', function (Blueprint $table) { $table->foreign(config('blame.created_by_field_name'))->references('id')->on('users'); $table->foreign(config('blame.updated_by_field_name'))->references('id')->on('users'); }); }
System User命令
当不带参数或参数使用时
php artisan blame:set:systemuser
此命令创建一个新的laravel用户,用于在没有通过Auth::user()检索到用户时使用。
当通过可选的"--key"参数传递值时
php artisan blame:set:systemuser --key=some_user_id
命令将检查是否存在具有该ID的用户,如果不存在,则尝试创建一个具有该ID的用户。
在这两种情况下,命令都将创建的系统用户的ID或主键设置在项目的.env文件中作为BLAME_SYSTEM_USER_ID。