Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Json Casted Laravel Package

novius/laravel-json-casted

Cast JSON columns in Laravel models with per-key casting. Define sub-key casts via a method or dedicated cast class; values are exposed as a Fluent object with proper types (e.g., Carbon dates). Supports PHP 8.2+ and Laravel 10+.

View on GitHub
Deep Wiki
Context7

Laravel Json Casted

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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nasirkhan/laravel-sharekit
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony