hnhdigital-os / laravel-model-schema
改变Eloquent模型提供属性的方式。
3.0.2
2023-09-07 01:37 UTC
Requires
- php: ^8.0
- hnhdigital-os/laravel-null-carbon: ~3.0
- laravel/framework: ^9.21|^10.0
Requires (Dev)
- illuminate/database: ^9.21|^10.0
- orchestra/testbench: ^7.0|^8.0
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2024-09-07 03:58:40 UTC
README
___ ___ _ _ _____ _
| \/ | | | | / ___| | |
| . . | ___ __| | ___| \ `--. ___| |__ ___ _ __ ___ __ _
| |\/| |/ _ \ / _` |/ _ \ |`--. \/ __| '_ \ / _ \ '_ ` _ \ / _` |
| | | | (_) | (_| | __/ /\__/ / (__| | | | __/ | | | | | (_| |
\_| |_/\___/ \__,_|\___|_\____/ \___|_| |_|\___|_| |_| |_|\__,_|
将铸造、可填充和隐藏属性合并到单个模式数组属性中,以及其他功能。
其他功能包括
- 针对您的模型属性进行自定义配置条目。
- 在您的模型中存储属性规则,并自动存储和验证模型数据。
- 现在可以使用此包进行自定义铸造!请参阅自定义铸造。
此包由澳大利亚精品开发商H&H|Digital开发。访问我们的hnh.digital。
文档
先决条件
- PHP >= 8.0.2
- Laravel >= 9.0
安装
通过composer
$ composer require hnhdigital-os/laravel-model-schema ~3.0
配置
启用模型
在任何给定的模型上启用模型。
use HnhDigital\ModelSchema\Model; class SomeModel extends Model { }
我们建议实现一个共享基模型,您可以从它扩展。
转换当前属性
模型的模式是通过一个受保护的属性实现的。
以下是一个例子
/** * Describe your model. * * @var array */ protected static $schema = [ 'id' => [ 'cast' => 'integer', 'guarded' => true, ], 'name' => [ 'cast' => 'string', 'rules' => 'max:255', 'fillable' => true, ], 'created_at' => [ 'cast' => 'datetime', 'guarded' => true, 'log' => false, 'hidden' => true, ], 'updated_at' => [ 'cast' => 'datetime', 'guarded' => true, 'hidden' => true, ], 'deleted_at' => [ 'cast' => 'datetime', 'rules' => 'nullable', 'hidden' => true, ], ];
确保父级启动在您的触发器之后发生,这样在触发器触发验证之前就会完成任何属性更改。
/** * Boot triggers. * * @return void */ public static function boot() { self::updating(function ($model) { // Doing something. }); parent::boot(); }
实现此包的模型如果未通过验证将抛出ValidationException异常。请确保捕获这些异常。
try { $user = User::create(request()->all()); } catch (HnhDigital\ModelSchema\Exceptions\ValidationException $exception) { // Do something about the validation. // You can add things to the validator. $exception->getValidator()->errors()->add('field', 'Something is wrong with this field!'); // We've implemented a response. // This redirects the same as a validator with errors. return $exception->getResponse('user::add'); }
自定义铸造
此包允许添加自定义铸造。只需创建一个特质,并在启动时注册铸造。
trait ModelCastAsMoneyTrait { /** * Cast value as Money. * * @param mixed $value * * @return Money */ protected function castAsMoney($value, $currency = 'USD', $locale = 'en_US'): Money { return new Money($value, $currency, $locale); } /** * Convert the Money value back to a storable type. * * @return int */ protected function castMoneyToInt($key, $value): int { if (is_object($value)) { return (int) $value->amount(); } return (int) $value->amount(); } /** * Register the casting definitions. */ public static function bootModelCastAsMoneyTrait() { static::registerCastFromDatabase('money', 'castAsMoney'); static::registerCastToDatabase('money', 'castMoneyToInt'); static::registerCastValidator('money', 'int'); } }
定义您的属性看起来像这样
... 'currency' => [ 'cast' => 'string', 'rules' => 'min:3|max:3', 'fillable' => true, ], 'total_amount' => [ 'cast' => 'money', 'cast-params' => '$currency:en_US', 'default' => 0, 'fillable' => true, ], ...
铸造参数包括一个辅助函数或本地模型方法。
例如 $currency:user_locale()
,或 $currency():$locale()
可用的自定义铸造
- 将属性铸造为货币
贡献
请参阅贡献指南。
只有符合所有标准的PR才会被接受。
报告问题
在报告问题时,请尽可能完整地填写包含的模板。不完整的问题可能会被忽略或关闭,如果没有足够的信息可以采取行动。
行为准则
请遵守和尊重包含的行为准则。
致谢
许可
MIT许可证(MIT)。有关更多信息,请参阅许可文件。