brainstud / has-identifier
该包的最新版本(v2.0.6)没有提供许可证信息。
将 HasIdentifier 添加到模型中以处理标识符的生成
v2.0.6
2024-06-04 07:01 UTC
Requires
- php: ^7.4 || ^8.2
- laravel/framework: ^8.0 || ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- orchestra/testbench: ^6.23
- phpunit/phpunit: ^9.5
README
通过添加 HasIdentifier
特性来自动填充模型的 identifier
安装
需要此包
composer require brainstud/has-identifier
用法
将 HasIdentifier
特性添加到您的模型中。
/** * @property string $identifier */ class Item extends Model { use HasIdentifier; protected $fillable = [ 'identifier' ]; }
创建时会自动填充标识符。
$model = Item::create(); echo $model->identifier;
使用不同的标识符属性
特性会使用您的模型中的 identifier
属性来生成标识符。您可以通过在模型上设置 identifierAttribute
来覆盖特性使用的属性。
/** * @property string $identifier */ class Item extends Model { use HasIdentifier; protected string $identifierAttribute = 'uuid'; protected $fillable = [ 'uuid' ]; }
按标识符查找
有多种方式可以通过标识符获取模型。
// Find method $model = Item::findByIdentifier($uuid); $model = Item::findOrFailByIdentifier($uuid); // Scope $model = Item::identifiedBy($uuid)->first();
测试
您可以使用以下命令运行测试:
composer test
许可证
has-identifier 是开源软件,遵循 MIT 许可证