jn-jairo/laravel-eloquent-cast 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jn-jairo/laravel-eloquent-cast

Composer 安装命令:

composer require jn-jairo/laravel-eloquent-cast

包简介

Cast for Laravel Eloquent.

README 文档

README

Total Downloads Latest Stable Version License

Cast for Laravel Eloquent

This package extends the built-in attribute casting with the jn-jairo/laravel-cast package.

Version Compatibility

Laravel Eloquent Cast
5.8.x 1.x
6.x 1.x
7.x 2.x
8.x 3.x
9.x 3.x
10.x 3.x
11.x 3.x
12.x 3.x

Installation

You can install the package via composer:

composer require jn-jairo/laravel-eloquent-cast

Usage

Use the trait \JnJairo\Laravel\EloquentCast\HasAttributesCast in a \Illuminate\Database\Eloquent\Model. In the $casts property of the model set the attribute name as the key and the type:format as the value.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use JnJairo\Laravel\EloquentCast\HasAttributesCast;

class Foo extends Model
{
    use HasAttributesCast;

    protected $casts = [
        'uuid' => 'uuid',
        'is_admin' => 'boolean',
        'created_at' => 'datetime:Y-m-d H:i:s',
    ];
}

The attribute is casted for a PHP type in the getter and/or with a suffix, for a database type in the setter, and for a json type when serialized to array/json.

Example

$foo = new Foo();
$foo->uuid = '72684d25-b173-468d-8d45-2a10b2cc3e9f';
$foo->is_admin = 1;
$foo->created_at = '2000-01-01 00:00:00';

print_r(gettype($foo->uuid));
// string

print_r(get_class($foo->uuid_));
// Ramsey\Uuid\Uuid

var_dump($foo->is_admin);
// bool(true)

print_r($foo->created_at);
Illuminate\Support\Carbon Object
(
    [date] => 2000-01-01 00:00:00.000000
    [timezone_type] => 3
    [timezone] => UTC
)

Configuration

Publish the configuration to config/eloquent-cast.php.

php artisan vendor:publish --provider=JnJairo\\Laravel\\EloquentCast\\EloquentCastServiceProvider

In the default configuration the attribute is casted for a PHP type in the getter and with the _ suffix, except for the type uuid which is casted only with the _ suffix, because to cast a primary/foreign key directly in the getter brakes the relations between the eloquent models.

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 21
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-02-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固