定制 hnhdigital-os/laravel-model-schema 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.

Latest Stable Version Total Downloads Latest Unstable Version Built for Laravel License Donate to this project using Patreon

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 hnhdigital-os/laravel-model-schema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 124
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-03