Download the PHP package synergitech/laravel-magic-enums without Composer
On this page you can find all versions of the php package synergitech/laravel-magic-enums. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download synergitech/laravel-magic-enums
More information about synergitech/laravel-magic-enums
Files in synergitech/laravel-magic-enums
Package laravel-magic-enums
Short Description A Laravel package that adds extra power to your PHP enums, and lets you use them in your frontend with type definitions.
License MIT
Homepage https://github.com/SynergiTech/laravel-magic-enums
Informations about the package laravel-magic-enums
Laravel Magic Enums
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.
Installing
You need both sides to get started.
Getting Started
-
Add the trait and interface from this package to your enums. We recommend you don't include sensitive information in your enums.
-
Generate an export of enums with
php artisan laravel-magic-enums:generate. This will create a file atresources/js/magic-enums/index.js. -
Use the exported enums in your frontend like so. Your IDE will infer any types from your enums:
-
During development, you can have Vite react automatically to changes in your enums. The
laravel-magic-enums/viteplugin provides a simple way to do this. Under the hood, this calls thephp artisan laravel-magic-enums:generatecommand, and can customise it. For example, here's how to use it with some customisations: - We recommend adding the exported enums to the
.gitignorefile.
Advanced Usage
Sub Enums
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:
Will create the output:
Extending
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.