artisanbr / generic-model
一个基于 Laravel 和其他框架的 Laravel eloquent-like 模型类,受 jenssegers/model 启发。
v2.0.1
2024-03-28 17:26 UTC
Requires
- illuminate/contracts: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/database: ^4.0|^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^4.0|^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
This package is auto-updated.
Last update: 2024-09-11 14:36:35 UTC
README
本包提供基于 Laravel Eloquent 结构的通用模型创建,包含对 jenssegers/model 包的改进和适配。
安装
composer require artisanbr/generic-model
版本
版本表
功能和适配
- 访问器和修改器
- 模型到数组和 JSON 的转换
- 数组/JSON 转换中的隐藏属性
- 受保护(guarded)和可填充(fillable)属性
- 在数组/JSON 转换中附加访问器和修改器
- 属性类型转换
您可以在 https://laravel.net.cn/docs/eloquent 了解 Eloquent 模型原始功能的相关信息。
示例
use Model\GenericModel; class User extends GenericModel { protected $hidden = ['password']; protected $guarded = ['password']; protected $casts = ['age' => 'integer']; public function save() { return API::post('/items', $this->attributes); } public function setBirthdayAttribute($value) { $this->attributes['birthday'] = strtotime($value); } public function getBirthdayAttribute($value) { return new DateTime("@$value"); } public function getAgeAttribute($value) { return $this->birthday->diff(new DateTime('now'))->y; } } $item = new User(array('name' => 'john')); $item->password = 'bar'; echo $item; // {"name":"john"}