arandu / laravel-safe-json-cast
A simple cast for Laravel Mui Admin Draft Js Plugin
v0.0.1
2024-01-14 03:06 UTC
Requires
- php: ^8.0
- illuminate/support: ^9.19
Requires (Dev)
- orchestra/testbench: ^7.35
- phpunit/phpunit: ^9.5.10
This package is auto-updated.
Last update: 2025-03-14 05:51:53 UTC
README
If you are using Laravel's native array/object cast, you may have noticed that it turns JS empty objects into empty arrays. This package provides an alternative to the native cast.
Installation
You can install the package via composer:
composer require arandu/laravel-safe-json-cast
Usage
use Arandu\LaravelSafeJsonCast\Json; class User extends Model { protected $casts = [ 'options' => Json::class, ]; } $user = User::create([ 'name' => 'John Doe', 'options' => [ 'foo' => 'bar', ], ]); $user->options->foo; // bar
Best Practices
It is advisable that you send casted fields as JSON strings from the frontend. This way you can avoid the problem of empty objects being converted to empty arrays. This package will handle JSON strings and convert them accordingly.
axios({ method: 'post', url: '/api/users', data: { name: 'John Doe', options: JSON.stringify(userOptions), } });
License
The MIT License (MIT). Please see License File for more information.