kamansoft/laravel-blame
Composer 安装命令:
composer require kamansoft/laravel-blame
包简介
A tool to easily work with the updated_by and created_by fields on your laravel eloquent models
README 文档
README
update_by and created_by fields in your laravel model ?
This is a laravel package that will ease the usage of the normally called created_by and update_by extra fields used to stablish responsabilites on records persistance create or update events, with similar fashion as the "timestamp fields" on Eloquent's models, it automatically fills the fields with the primary key of the currently logged user or with a preconfigured system user id.
Requirements
This package was build taking in mind that you use laravel with an eloquent user model class as auth provider. So before installing, you must make sure that your user eloquent model is set on the providers section of your auth config file like this:
//config/auth.php 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, // <-- this is the important part ], ],
Laravel version.
The Lasted version of this package was tested with laravel 9. Should be compatible with versions above also it should work with laravel 8 too, if you find any issue please open an issue on github
Installation
You can install the package via composer:
composer require kamansoft/laravel-blame
Sometimes your laravel application will perform actions that persist records on the database without an authenticated or logged-in user, You can call this user as System User or any other name you want, but you must specify its id or primary key.
To do so, you can run the system user command or the package install comand:
php artisan blame:install
The above command will publish the package config file, and it will run the system user command with no arguments.
Usage
You must first add the update_by and created_by fields to the table's migration field to use it in your models.
//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(); }); }
Then make use of the Modelblamer trait on your eloquent models, and its booting method will take care of the rest. Also for the seek of a good practice let your model implement the BlameableInterface on your models. Then your model should look something like this
//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
Commands
This package ships two artisan commands as tools to ease the package usage simplifying a repetitive task like creating or updating the system user or adding the updated by and created by fields to your model's table.
Blame FIelds Migration command
This command is primary intended to be used when you need to update an existing table, adding the created_by and updated_by fields; it needs an argument with the name of the table to update.
php artisan blame:make:migration some_table_name
When run, the above command will create a new migration file in the database/migrations folder with the name add_blame_fields_to_some_table_name_table with a content similar to this:
//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 Command
When used with no param or arguments:
php artisan blame:set:systemuser
This command creates a new laravel user to be used when no user can be retrived with Auth::user().
When run with a value through the optional "--key" param:
php artisan blame:set:systemuser --key=some_user_id
The command will check if a user with that id exists, and if not, it will try to create one with that id.
In both cases, the command will set the created system user primary key or id in the project .env file as BLAME_SYSTEM_USER_ID.
kamansoft/laravel-blame 适用场景与选型建议
kamansoft/laravel-blame 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.6k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2022 年 12 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「kamansoft」 「laravel-blame」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kamansoft/laravel-blame 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kamansoft/laravel-blame 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kamansoft/laravel-blame 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
Turn any PDF into a Pingen-ready A4 letter (generated address cover page + A4 normalisation + safe margins) and send it through the Pingen print & mail API. Laravel-first.
统计信息
- 总下载量: 2.6k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-28