supportpal/eloquent-model

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

v1.0.0 2024-05-29 17:16 UTC

This package is auto-updated.

Last update: 2024-08-29 17:51:16 UTC


README

Build Status Coverage Status

此模型提供了一个类似于 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"}