mennovanhout/laravel-model-constants
Composer 安装命令:
composer require mennovanhout/laravel-model-constants
包简介
This package creates constants for column names, this can then be used instead of strings to reduce typo's errors and improve coding with your IDE
README 文档
README
This package will generate constant files for your models to reduce typo's and the need of debugging to find out what attributes are available.
Benefits
- This package is ready to be installed and to forget about, no maintenance or commands you have to learn.
- This packages listens to the Laravel migrations ended event (Files automatically get updated/generated).
- When a column is removed you will get IDE errors.
- Fewer typo's.
- These constants are widely available throughout your whole codebase instead of having to type string.
- This package is created with all design patterns in mind. Whether you use domain driven design or Laravels default approach.
How to install it
composer require mennovanhout/laravel-model-constants
How to use it
This packages hooks into Laravels migration system and will generate the files after each migration or batch of migrations.
You can run it manually with: artisan model:constants.
If you want to remove files generated by this packages you can do that with: artisan model:constants-clean.
Example: Generated constant file
<?php namespace Domain\Authentication\Models; use MennoVanHout\LaravelModelConstants\Types\ModelAttributes; class UserAttributes extends ModelAttributes { const ID = 'id'; const NAME = 'name'; const EMAIL = 'email'; const EMAIL_VERIFIED_AT = 'email_verified_at'; const PASSWORD = 'password'; const REMEMBER_TOKEN = 'remember_token'; const CREATED_AT = 'created_at'; const UPDATED_AT = 'updated_at'; }
Example Model
<?php namespace Domain\Authentication\Models; class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable; protected $fillable = [ UserAttributes::NAME, UserAttributes::EMAIL, UserAttributes::PASSWORD, ]; protected $hidden = [ UserAttributes::PASSWORD, UserAttributes::REMEMBER_TOKEN, ]; protected $casts = [ UserAttributes::EMAIL_VERIFIED_AT => 'datetime', ]; }
Todo:
- Add config file.
- Be able to put the generated constant files in another directory.
- Add an option to also generated relations in the constants.
- Add an option to also generated custom attributes in the constants.
mennovanhout/laravel-model-constants 适用场景与选型建议
mennovanhout/laravel-model-constants 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 515 次下载、GitHub Stars 达 12, 最近一次更新时间为 2023 年 03 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mennovanhout/laravel-model-constants 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mennovanhout/laravel-model-constants 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 515
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-14