New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

intrfce/laravel-frontend-enums

Stop using 'magic' strings/numbers in your frontend Javascript and use your actual application enums instead.
38,355 23
Install
composer require intrfce/laravel-frontend-enums
Latest Version:v1.1.0
PHP:^8.4.0
Laravel:11 · 12 · 13
Older releases:10
License:MIT
Last Updated:Jun 1, 2026
Links: GitHub  ·  Packagist
Maintainer: danmatthews

Laravel Frontend Enums

Latest Version on Packagist Total Downloads GitHub Actions

Publish your PHP enums to the front-end of your application, so you can refer to them in your JavaScript code.

This means less reliance on "magic strings".


// Before:

if (myValue === 'name') {
  // Do something
}

// After
import {UserProfileField} from './UserProfileField.enum.js';

if (myValue === UserProfileField.Name) {
  // Do something.
}

Installation

You can install the package via composer:

composer require intrfce/laravel-frontend-enums

Usage

Just add the attribute!

Add the #[PublishEnum] attribute to any enum you want published to the frontend - it's that easy.

use Intrfce\LaravelFrontendEnums\Attributes\PublishEnum;

#[PublishEnum]
enum MyEnum: string {
    case Foo = 'foo';
    case Bar = 'bar';
}

Then run the publish command:

php artisan publish:enums-to-javascript

Your enums will be waiting at resources/js/Enums with the extension .enum.js:

import {MyEnum} from './Enums/MyEnum.enum.js';

console.log(MyEnum.Foo); // 'foo'

Configuration

Publish the config file to customise the output path and discovery directories:

php artisan vendor:publish --tag=config --provider="Intrfce\LaravelFrontendEnums\LaravelFrontendEnumsServiceProvider"
// config/laravel-frontend-enums.php
return [
    
    // Customise the output directory.
    'publish_to' => resource_path('js/Enums'),
    
    // Customise the folders scanned for enum classes
    'discover_in' => [
        app_path(),
    ],
    
    // Always output as typescript enums.
    'as_typescript' => true,
    
];

The discover_in array supports glob patterns, which is useful for modular or monorepo layouts:

'discover_in' => [
    app_path(),
    base_path('app-modules/*/src'),
],

Typescript Support

Enable TypeScript output globally via the config file:

'as_typescript' => true,

Or override per-enum using the attribute:

#[PublishEnum(asTypescript: true)]  // Force TypeScript for this enum
#[PublishEnum(asTypescript: false)] // Force JavaScript for this enum
#[PublishEnum]                      // Follow the global config setting

TypeScript enums are output as .ts files:

export enum MyEnum {
    Foo = "foo",
    Bar = "bar",
}

Automatically generate javascript files on change.

You can use the vite-plugin-watch package from lepikhinb to automatically generate your javascript files when you make changes to your PHP enums:

npm install -D vite-plugin-watch

Then add the plugin to your vite.config.js:

import { defineConfig } from "vite"
import { watch } from "vite-plugin-watch"

export default defineConfig({
  plugins: [ 
    watch({
      pattern: "app/Enums/**/*.php",
      command: "php artisan publish:enums-to-javascript",
    }),
  ],
})

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email dan@danmatthews.me instead of using the issue tracker.

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...

648,266,899 9,701
laravel/framework

The Laravel Framework.

604,216,237 34,982
laravel/tinker

Powerful REPL for the Laravel framework.

512,508,631 7,443
laravel/serializable-closure

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

429,221,767 607
nunomaduro/collision

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

403,217,078 4,664

Version History

Version Released PHP Laravel License
v1.1.0 ^8.4.0 ^11.0 || ^12.0 || ^13.0 MIT
v1.0.1 ^8.4.0 ^11.0 || ^12.0 MIT
v1.0.0 ^8.4.0 ^11.0 || ^12.0 MIT
v0.7.8 ^8.4.0 ^11.0 || ^12.0 MIT
v0.7.7 ^8.4.0 ^11.0 || ^12.0 MIT