timolake/model

一个类似于 Laravel eloquent 的模型类,适用于 Laravel 和其他框架

1.0.0 2023-03-01 17:39 UTC

This package is auto-updated.

Last update: 2024-09-29 20:58:32 UTC


README

Build Status Coverage Status

此模型提供了一个 Laravel eloquent 类似的基类,可用于在 Laravel 或其他框架中构建自定义模型。

特性

  • 访问器和修改器
  • 模型到数组和 JSON 的转换
  • 数组/JSON 转换中的隐藏属性
  • 受保护属性和可填充属性
  • 将访问器和修改器添加到数组/JSON 转换中
  • 属性转换

您可以在此处了解有关这些功能和原始 Eloquent 模型的更多信息:https://laravel.net.cn/docs/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"}