joshcadman01 / jenssegers-model
一个 Laravel 类似 Eloquent 的模型类,适用于 Laravel 及其他框架
1.0.0
2024-04-02 14:14 UTC
Requires
- illuminate/contracts: ^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
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-03 14:38:09 UTC
README
此模型提供了一个 Laravel 类似 Eloquent 的基类,可用于在 Laravel 或其他框架中构建自定义模型。
特性
- 访问器和修改器
- 模型到数组和 JSON 的转换
- 在数组和 JSON 转换中隐藏属性
- 受保护和可填充的属性
- 将访问器和修改器添加到数组和 JSON 转换中
- 属性类型转换
您可以在 https://laravel.net.cn/docs/eloquent 上阅读更多关于这些特性以及原始 Eloquent 模型的信息。
安装
使用 composer 安装
composer require jenssegers/model
示例
use Jenssegers\Model\Model; class User extends Model { 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"}