saas-laravel/laravel-enums-to-json

This package generates a json file off of values from enum
49,403 6
Install
composer require saas-laravel/laravel-enums-to-json
Latest Version:4.0.0
PHP:^8.3
Laravel:12
Older releases:10 · 11
License:MIT
Last Updated:Jan 22, 2026
Links: GitHub  ·  Packagist
Maintainer: saas-laravel

Laravel Attributed Enums to json

The idea for this package is to streamline the process of sharing enums values between backend and frontend.

If you use Laravel as a backend for a frontend framework like Nuxt or Next, and they both live in the same project, you can utilize it to generate a json file from your enums.

Installation

You can install the package via composer:

composer require saas-laravel/laravel-enums-to-json

You can publish the config file with:

php artisan vendor:publish --tag="laravel-enums-to-json"

This is the contents of the published config file:

return [
    // The disk, defined in filesystem.php, where json files will be stored
    'disk' => 'public',

    // The folder on that disk where json will be generated
    'path' => '/shared',

    'enum_locations' => [
        app_path(),
    ],
];

Usage

In order to use the package, you need to add SaasLaravel\LaravelEnumsToJson\Attributes\EnumToJson attribute to your enum

Example usage:

<?php

namespace App\Enums;

use SaasLaravel\LaravelEnumsToJson\Attributes\EnumToJson; 

#[EnumToJson] //add attribute
enum CastType: int
{
    case Boolean = 0;
    case Integer = 1;
    case String = 2;

    // (optional) A way to customize the generated file name
    public static function jsonFileName(): string
    {
        return 'cast';
    }
}

Then you'd run a command to generate the json files.

php artisan enum-to-json:generate

Customizing the name

By default, it generates a file named as a normalized version of the enum name - that'd be cast_type.json if the example above didn't contain the jsonFileName method. However, because it is present, it'd generate cast.json instead.

Testing

composer test

Credits

License

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

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

644,549,754 9,698
laravel/framework

The Laravel Framework.

598,760,099 34,966
laravel/tinker

Powerful REPL for the Laravel framework.

507,193,856 7,441
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

423,477,751 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

399,241,428 4,660

Version History

Version Released PHP Laravel License
4.0.0 ^8.3 ^12.0 MIT
3.0.0 ^8.1 ^11.0 MIT
2.1.0 ^8.1 ^10.0 MIT
2.0.2 ^8.1 ^10.0 MIT
1.0.0 ^8.1 ^10.0 MIT