定制 novius/laravel-json-casted 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

novius/laravel-json-casted

最新稳定版本:1.2.0

Composer 安装命令:

composer require novius/laravel-json-casted

包简介

A package to cast json fields, each sub-keys is castable

README 文档

README

Novius CI Packagist Release License: AGPL v3

Introduction

A package to cast json fields, each sub-keys is castable

Requirements

  • PHP >= 8.2
  • Laravel 10.0

Installation

You can install the package via composer:

composer require novius/laravel-json-casted

If you use laravel-ide-helper, add ModelHasJsonWithCastsHook in its configuration file :

    'model_hooks' => [
        // ...
        \Novius\LaravelJsonCasted\Hooks\ModelHasJsonWithCastsHook::class,
    ],

Usage

Define casts by a method

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Novius\LaravelJsonCasted\Services\JsonCasted;

class Post extends Model {

    protected $casts = [
        'extras' => JsonCasted::class.':getExtrasCasts',
    ];
    
    public function getExtrasCasts(): array
    {
        return [
            'date' => 'date:Y-m-d',
        ];
    }
}

Define casts by a class

namespace App\Casts;

use Novius\LaravelJsonCasted\Services\JsonCasted;

class Extras extends JsonCasted {

    protected static array $casts = [
        'date' => 'date:Y-m-d',
    ];
}
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Casts\Extras;

class Post extends Model {

    protected $casts = [
        'extras' => Extras::class,
    ];
}

Use casted field

    $model = Post::first();
    // $model->extras is now a Fluent instance 
    // $model->extras->date is a now Carbon class 
    $model->extras->date->lt(now());

CS Fixer

Lint your code with Laravel Pint using:

composer run cs-fix

Licence

This package is under GNU Affero General Public License v3 or (at your option) any later version.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: AGPL-3.0-or-later
  • 更新时间: 2024-12-13