wildside/userstamps
Composer 安装命令:
composer require wildside/userstamps
包简介
Laravel Userstamps provides an Eloquent trait which automatically maintains `created_by` and `updated_by` columns on your model, populated by the currently authenticated user in your application.
README 文档
README
About Laravel Userstamps
Laravel Userstamps provides an Eloquent trait which automatically maintains created_by and updated_by columns on your model, populated by the currently authenticated user in your application.
When using the Laravel SoftDeletes trait, a deleted_by column is also handled by this package.
Installing
This package requires Laravel 9 or later running on PHP 8.2 or higher.
This package can be installed using composer:
composer require wildside/userstamps
Usage
Your model will need to include a created_by and updated_by column, defaulting to null.
If using the Laravel SoftDeletes trait, it will also need a deleted_by column.
The column type should match the type of the ID column in your user's table.
Userstamp columns can be created using:
$table->userstamps(); $table->userstampSoftDeletes();
Equivalent methods are also available when working with UUIDs.
$table->userstampsUuid(); $table->userstampsUuidSoftDeletes();
You can now load the trait within your model, and userstamps will automatically be maintained:
use Mattiverse\Userstamps\Traits\Userstamps; class Foo extends Model { use Userstamps; }
Optionally, should you wish to override the names of the created_by, updated_by or deleted_by columns, you can do so by setting the appropriate class constants on your model. Ensure you match these column names in your migration.
use Mattiverse\Userstamps\Traits\Userstamps; class Foo extends Model { use Userstamps; const CREATED_BY = 'alt_created_by'; const UPDATED_BY = 'alt_updated_by'; const DELETED_BY = 'alt_deleted_by'; }
When using this trait, helper relationships are available to let you retrieve the user who created, updated and deleted (when using the Laravel SoftDeletes trait) your model.
$model->creator; // the user who created the model $model->editor; // the user who last updated the model $model->destroyer; // the user who deleted the model
Methods are also available to temporarily stop the automatic maintaining of userstamps on your models:
$model->stopUserstamping(); // stops userstamps being maintained on the model $model->startUserstamping(); // resumes userstamps being maintained on the model
Resolving Users
By default users will be resolved using the Laravel Auth::id() method, to return the ID of the currently authenticated user.
More advanced use-cases are supported with a custom resolution strategy.
In this example, a custom resolution method is called to retrieve the ID.
use Mattiverse\Userstamps\Userstamps; Userstamps::resolveUsing( fn () => auth()->user()->customUserIdResolutionMethod() );
The Userstamps::resolveUsing method is likely best suited to the boot method of AppServiceProvider.
Workarounds
This package works by hooking into Eloquent's model event listeners, and is subject to the same limitations of all such listeners.
When you make changes to models that bypass Eloquent, the event listeners won't be fired and userstamps will not be updated.
Commonly this will happen if bulk updating or deleting models, or their relations.
In this example, model relations are updated via Eloquent and userstamps will be maintained:
$model->foos->each(function ($item) { $item->bar = 'x'; $item->save(); });
However in this example, model relations are bulk updated and bypass Eloquent. Userstamps will not be maintained:
$model->foos()->update([ 'bar' => 'x', ]);
As a workaroud to this issue two helper methods are available - updateWithUserstamps and deleteWithUserstamps. Their behaviour is identical to update and delete, but they ensure the updated_by and deleted_by properties are maintained on the model.
You generally won't have to use these methods, unless making bulk updates that bypass Eloquent events.
In this example, models are bulk updated and userstamps will not be maintained:
$model->where('name', 'foo')->update([ 'name' => 'bar', ]);
However in this example, models are bulk updated using the helper method and userstamps will be maintained:
$model->where('name', 'foo')->updateWithUserstamps([ 'name' => 'bar', ]);
License
This open-source software is licensed under the MIT license.
wildside/userstamps 适用场景与选型建议
wildside/userstamps 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.87M 次下载、GitHub Stars 达 752, 最近一次更新时间为 2016 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「eloquent」 「userstamps」 「created_by」 「updated_by」 「deleted_by」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wildside/userstamps 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wildside/userstamps 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wildside/userstamps 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Help to manage meta data on Laravel Eloquent model
A package to cast json fields, each sub-keys is castable
Package to add userstamps to models.
Automatically track the user who created, updated, or deleted Eloquent models in Laravel, with database macros and model traits.
统计信息
- 总下载量: 1.87M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 754
- 点击次数: 14
- 依赖项目数: 24
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2016-03-10