amol/laravel-jsonsafe

Mutable JSON column casting for Laravel Eloquent

Maintainers

Package info

github.com/AmolKumarGupta/laravel-jsonsafe

pkg:composer/amol/laravel-jsonsafe

Transparency log

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

v0.1.1 2026-08-22 10:33 UTC

This package is auto-updated.

Last update: 2026-08-22 10:38:40 UTC


README

Laravel JsonSafe

Latest Version on Packagist run-tests

Mutable JSON column casting for Laravel Eloquent. Supports JSON Schema validation via opis/json-schema.

Installation

You can install the package via composer:

composer require amol/laravel-jsonsafe

Usage

Basic casting

Cast a JSON column to JsonSafe in your model:

use Amol\LaravelJsonSafe\JsonSafe;

class User extends Model
{
    protected $casts = [
        'extras' => JsonSafe::class,
    ];
}

Read and write nested keys directly — changes persist on save():

$user->extras['theme'] = 'dark';
$user->save();

$user->extras['theme']; // 'dark'

JSON Schema validation

Define a public schemaOf{Key}() method on your model to enable validation. The column key is converted to StudlyCase and prefixed with schemaOf (e.g., preferencesschemaOfPreferences()).

use Amol\LaravelJsonSafe\JsonSafe;

class User extends Model
{
    protected $casts = [
        'preferences' => JsonSafe::class,
    ];

    public function schemaOfPreferences(): array
    {
        return [
            'type' => 'object',
            'properties' => [
                'appearance' => [
                    'type' => 'object',
                    'properties' => [
                        'theme' => [
                            'type' => 'string',
                            'enum' => ['light', 'dark', 'system'],
                        ],
                        'fontSize' => [
                            'type' => 'integer',
                            'minimum' => 10,
                            'maximum' => 24,
                        ],
                    ],
                ],
            ],
        ];
    }
}

If the data violates the schema, an Exception is thrown before the database write:

$user->preferences['appearance']['theme'] = 'invalid';
$user->save(); // throws Exception

Key mapping rules:

  • preferencesschemaOfPreferences()
  • my_columnschemaOfMyColumn()
  • UPPERCASEschemaOfUppercase()

Columns without a schema method are not validated.

Schemas follow the JSON Schema standard via opis/json-schema. You can use any supported keywords (type, properties, enum, required, minimum, maximum, pattern, etc.).

Serialization

toArray() and toJson() return the underlying plain array:

$user->extras->toArray(); // ['theme' => 'dark']

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

License

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