sfneal/casts

类型转换属性访问器和修改器的Eloquent模型替代实现

2.0.0 2024-04-16 01:49 UTC

This package is auto-updated.

Last update: 2024-09-20 17:11:52 UTC


README

Packagist PHP support Latest Version on Packagist Build Status StyleCI Scrutinizer Code Quality Total Downloads

类型转换属性访问器和修改器的Eloquent模型替代实现

安装

您可以通过composer安装此包

composer require sfneal/casts

使用

为了使用属性类型转换,请在定义应转换为自定义类型的属性之前,将HasCustomCasts特性添加到您的模型中。

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Sfneal\Casts\CarbonCast;
use Sfneal\Casts\NewlineCast;
use Sfneal\Casts\NullableIntArrayCast;
use Sfneal\LaravelCustomCasts\HasCustomCasts;

class People extends Model
{
    use HasCustomCasts;

    protected $table = 'people';
    protected $primaryKey = 'person_id';

    protected $fillable = [
        'person_id',
        'name_first',
        'name_last',
        'email',
        'birthday',
        'bio',
        'favorites',
    ];

    /**
     * @var array Attributes that should be type cast
     */
    protected $casts = [
        'birthday' => CarbonCast::class,
        'bio' => NewlineCast::class,
        'favorites' => NullableIntArrayCast::class,
    ];
}

测试

composer test

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献指南

安全性

如果您发现任何安全问题,请通过stephen.neal14@gmail.com发送电子邮件,而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件

PHP包模板

此包是用PHP包模板生成的。