承接 joecianflone/modelproperties 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

joecianflone/modelproperties

Composer 安装命令:

composer require joecianflone/modelproperties

包简介

Make the DX of Eloquent Models better

README 文档

README

As a developer, you may have experienced the cognitive load of dealing with arrays to define various model behaviors in Laravel, such as fillable, guarded, casting, default values, and hidden properties. I found myself wondering if there was a more streamlined way to handle this. What if we could use PHP attributes on the class itself to define these properties? This idea led me to explore this approach, which I share in this video

Support Me

Buy me a coffee or something, but more importantly would be if you used this and like it, if you'd talk about this and also tell me if you have any issues with it.

Installation

composer require joecianflone/modelproperties

Usage

Here's a standard User model where you've got some fillable fields and a few casts and whatnot:

class User extends Model {

    protected $fillable = ['name', 'email', 'password', 'role', 'is_active'];

    protected $hidden = ['password', 'remember_token'];

    protected $casts = [
        'id' => 'string',
        'role' => UserRole::class
        'is_active' => 'boolean',
        'email_activated_at' => 'datetime',
        'remember_token' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    protected $attributes = [
        'role' => UserRole::STANDARD
        'is_active' => true,
    ];
}

Again, my gripe here is that it takes a bit of work to understand all the fields this model has. It's pretty easy to miss that the id is a string if you were looking quickly...and oh crap, I forgot to turn off auto-incrementing did you catch that? How about the fact that the password should be cast and I forgot that one too

So using the model properties it would now look like this:

#[ModelProperties(
    id:  ['string', 'is:primary' 'auto-increment' => false],         
    name:  ['string', 'is:fillable'],      
    email:  ['string', 'is:fillable'],
    password:  ['hashed', 'is:hidden|fillable'],   
    role: [UserRole::class => UserRole::STANDARD], 
    is_active:          ['boolean' => true],
    remember_token:     ['string', 'is:hidden'],
    email_verified_at:  ['datetime'],
    created_at:         ['datetime'],
    updated_at:         ['datetime'],
)]
class User extends Model {

    use HasModelProperties;

}

That's it. Add the HasModelProperties trait and you're good to start using the ModelProperties attribute and have a better DX.

Config Settings

Model properties has a few configurations you can change and they're publishable.

$ php artisan vendor:publish --provider="JoeCianflone\ModelProperties\ModelPropertiesServiceProvider"

Mass Assignment

Turn this off at your own risk! By default, this will set $guarded = ['*'] you should leave it this way.

    [
         'mass_assignment_protection' => true, // default
    ]

Default Mass Assignment Mode

Now if you don't want to set all your properties to either guareded or fillable on all your models you can change this to be either 'guarded' or 'fillable' and then you don't have to worry about being explicit.

NOTE: with mass_assignment_protection turned on, you really never have to expliclty set something to guarded they'll really be discarded because $guarded = ['*']. If you DO turn off mass_assignment_protection you're going to NEED to set this to guarded or else we'll throw an exception with this set to either 'none' or 'fillable'.

Honestly, think twice before doing that. Your best bet here is to leave mass_assignment_protection set to true and, if anything, set this to fillable. That's basically Laravel's default behavior and setting all the props over to the fillable array.

    [
        // values could be 'none' | 'fillable' | 'guarded'
        'default_property_assignment' => 'none', // default 
    ]

Explicitly Cast Strings

    [
        'explicity_cast_strings' => false // default
    ]

In the above examples, see how how told the model that some properties were strings? You don't actually need to cast strings as strings in laravel, but some people like doing that, so if you'd like to be explicit, set this to true.

How it works

Under the hood, we're not doing too many fancy things. At the end of the day, the attribute will get transformed into a PHP object and we "just use" all the standard Laravel model variables to populate fillable, guarded and whatever else. The only magic here, if there is any, is Laravels own built-in ability to automatically execute a method in a trait.

So what is this doing? Mostly parsing the attributes and dumping them in the right places. It adds no extra cruft to the model and if you were to dd() a model that uses ModelProperties and a model that does not, you'd see no difference in the object created.

Testing

This plugin uses Pest for our Unit and Architecture tests, if you'd like to run them yourself you can use...

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

joecianflone/modelproperties 适用场景与选型建议

joecianflone/modelproperties 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.14k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「attributes」 「laravel」 「models」 「eloquent」 「model-attributes」 「developer-experience」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 joecianflone/modelproperties 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 joecianflone/modelproperties 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-02-17