hnhdigital-os/laravel-model-schema
Composer 安装命令:
composer require hnhdigital-os/laravel-model-schema
包简介
Changes how the Eloquent Model provides attributes.
README 文档
README
___ ___ _ _ _____ _
| \/ | | | | / ___| | |
| . . | ___ __| | ___| \ `--. ___| |__ ___ _ __ ___ __ _
| |\/| |/ _ \ / _` |/ _ \ |`--. \/ __| '_ \ / _ \ '_ ` _ \ / _` |
| | | | (_) | (_| | __/ /\__/ / (__| | | | __/ | | | | | (_| |
\_| |_/\___/ \__,_|\___|_\____/ \___|_| |_|\___|_| |_| |_|\__,_|
Combines the casts, fillable, and hidden properties into a single schema array property amongst other features.
Other features include:
- Custom configuration entries against your model attributes.
- Store your attribute rules against your model and store and validate your model data automatically.
- Custom casting is now possible using this package! See Custom casts.
This package has been developed by H&H|Digital, an Australian botique developer. Visit us at hnh.digital.
Documentation
Prerequisites
- PHP >= 8.0.2
- Laravel >= 9.0
Installation
Via composer:
$ composer require hnhdigital-os/laravel-model-schema ~3.0
Configuration
Enable the model
Enable the model on any given model.
use HnhDigital\ModelSchema\Model; class SomeModel extends Model { }
We recommend implementing a shared base model that you extend.
Convert your current properties
The schema for a model is implemented using a protected property.
Here's an example:
/** * 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, ], ];
Ensure the parent boot occurs after your triggers so that any attribute changes are done before this packages triggers the validation.
/** * Boot triggers. * * @return void */ public static function boot() { self::updating(function ($model) { // Doing something. }); parent::boot(); }
Models implementing this package will now throw a ValidationException exception if they do not pass validation. Be sure to catch these.
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'); }
Custom casts
This package allows the ability to add custom casts. Simply create a trait, and register the cast on boot.
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'); } }
Defining your attributes would look like this:
... 'currency' => [ 'cast' => 'string', 'rules' => 'min:3|max:3', 'fillable' => true, ], 'total_amount' => [ 'cast' => 'money', 'cast-params' => '$currency:en_US', 'default' => 0, 'fillable' => true, ], ...
Casting parameters would include a helper function, or a local model method.
eg $currency:user_locale(), or $currency():$locale()
Available custom casts
- Cast the attribute to Money
Contributing
Please review the Contribution Guidelines.
Only PRs that meet all criterium will be accepted.
Reporting issues
When reporting issues, please fill out the included template as completely as possible. Incomplete issues may be ignored or closed if there is not enough information included to be actionable.
Code of conduct
Please observe and respect all aspects of the included Code of Conduct.
Credits
License
The MIT License (MIT). Please see License File for more information.
hnhdigital-os/laravel-model-schema 适用场景与选型建议
hnhdigital-os/laravel-model-schema 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 124 次下载、GitHub Stars 达 12, 最近一次更新时间为 2018 年 02 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「attributes」 「laravel」 「illuminate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hnhdigital-os/laravel-model-schema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hnhdigital-os/laravel-model-schema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hnhdigital-os/laravel-model-schema 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Convert has() and whereHas() constraints to non-dependent subqueries.
A library that allows you to easily use the PHP-VCR library in your PHPUnit tests.
This Package provides some usefully console features like the attribute syntax for arguments and options, validation, auto ask and casting.
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
This package provides a flexible way to add Role-based Permissions to Laravel
This package provides a flexible way to add Role-based Permissions to Laravel
统计信息
- 总下载量: 124
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-03