Looking to hire Laravel developers? Try LaraJobs

laravel-safe-json-cast maintained by arandu

Description
A simple cast for Laravel Mui Admin Draft Js Plugin
Author
Last update
2024/01/14 04:06 (dev-master)
License
Links
Downloads
547

Comments
comments powered by Disqus

Laravel Safe Json Cast

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.