
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
laravel-magic-enums
Advanced tools
A Laravel package that adds extra power to your PHP enums, and lets you use them in your frontend with type definitions.
Have you ever wanted to reference your PHP enums in your frontend code but ended up (or didn't want to end up) duplicating them manually? Well here is your answer.
You need both sides to get started.
$ composer require synergitech/laravel-magic-enums
$ npm install --save laravel-magic-enums
<?php
namespace App\Enums;
use SynergiTech\MagicEnums\Interfaces\MagicEnum;
use SynergiTech\MagicEnums\Traits\HasMagic;
enum YourEnum: string implements MagicEnum
{
use HasMagic;
...
Generate an export of enums with php artisan laravel-magic-enums:generate
. This will create a file at resources/js/magic-enums/index.js
.
Use the exported enums in your frontend like so. Your IDE will any types from your enums:
import { enums } from 'resources/js/magic-enums/index.js';
const { TestingEnums } = enums;
laravel-magic-enums/vite
plugin provides a simple way to do this. Under the hood, this calls the php artisan laravel-magic-enums:generate
command, and can customise it. For example, here's how to use it with some customisations:import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';
import { laravelMagicEnums } from "laravel-magic-enums/vite";
export default defineConfig({
plugins: [
laravel({
...
}),
vue({
...
}),
laravelMagicEnums({
input: 'app/Enums',
output: 'resources/js/my-enums',
format: true,
}),
],
...
.gitignore
file.You may choose to have an array within your enum of a subset of the values for a specific purpose or grouping.
If you use the PHP attribute SynergiTech\MagicEnums\Attributes\AppendConstToMagic
, then an extra enum representing this will be available in the frontend.
You may also have an array which maps some or all of the values of the enum to a different string.
If you use the PHP attribute SynergiTech\MagicEnums\Attributes\AppendValueToMagic
, then an extra enum representing this will be available in the frontend.
For example:
<?php
namespace App\Enums;
use SynergiTech\MagicEnums\Attributes\AppendConstToMagic;
use SynergiTech\MagicEnums\Attributes\AppendValueToMagic;
use SynergiTech\MagicEnums\Interfaces\MagicEnum;
use SynergiTech\MagicEnums\Traits\HasMagic;
enum TestingEnum: string implements MagicEnum
{
use HasMagic;
case First = 'first';
case Second = 'second';
case Third = 'third';
#[AppendConstToMagic]
public const JUST_ONE = [
self::First,
];
#[AppendValueToMagic]
public const COLOUR = [
self::First->value => 'red',
];
}
Will create the output:
TestingEnum: {
First: {
"name": "First",
"value": "first",
"colour": "red"
},
Second: {
"name": "Second",
"value": "second",
"colour": null
},
Third: {
"name": "Third",
"value": "third",
"colour": null
}
},
TestingEnumJustOne: {
First: {
"name": "First",
"value": "first",
"colour": "red"
}
}
If you wish to have more control over appending values to your magic enums for the frontend, you can extend the current trait using something along the lines of app/Traits/CustomMagic.php as long as you always follow the interface and provide the function.
FAQs
A Laravel package that adds extra power to your PHP enums, and lets you use them in your frontend with type definitions.
The npm package laravel-magic-enums receives a total of 395 weekly downloads. As such, laravel-magic-enums popularity was classified as not popular.
We found that laravel-magic-enums demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.