novius / laravel-json-casted
A package to cast json fields, each sub-keys is castable
1.0.1
2024-12-19 11:32 UTC
Requires
- php: >=8.2
- illuminate/database: ^10.0 || ^11.0
- illuminate/support: ^10.0 || ^11.0
Requires (Dev)
- laravel/pint: ^1.17.0
- orchestra/testbench: ^9.0
- pestphp/pest: ^2.34.9
README
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
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 $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->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.