{"id":24199072,"url":"https://github.com/isap-ou/laravel-enum-helpers","last_synced_at":"2025-09-21T22:31:20.496Z","repository":{"id":259221575,"uuid":"876768598","full_name":"isap-ou/laravel-enum-helpers","owner":"isap-ou","description":"This package brings some helpers of native PHP Enums to Laravel","archived":false,"fork":false,"pushed_at":"2025-02-27T08:39:18.000Z","size":338,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-01T12:51:26.584Z","etag":null,"topics":["enum","enums","filament","filamentphp","laravel","php"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/isap-ou/laravel-enum-helpers","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/isap-ou.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-22T14:23:19.000Z","updated_at":"2025-02-27T08:39:01.000Z","dependencies_parsed_at":"2024-12-06T00:18:17.631Z","dependency_job_id":"9335cc51-4ee3-4c38-a7aa-2fef445ae1c1","html_url":"https://github.com/isap-ou/laravel-enum-helpers","commit_stats":null,"previous_names":["isap-ou/laravel-enum-helpers"],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/isap-ou/laravel-enum-helpers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isap-ou%2Flaravel-enum-helpers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isap-ou%2Flaravel-enum-helpers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isap-ou%2Flaravel-enum-helpers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isap-ou%2Flaravel-enum-helpers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/isap-ou","download_url":"https://codeload.github.com/isap-ou/laravel-enum-helpers/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isap-ou%2Flaravel-enum-helpers/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276318278,"owners_count":25621648,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-21T02:00:07.055Z","response_time":72,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["enum","enums","filament","filamentphp","laravel","php"],"created_at":"2025-01-13T20:20:00.519Z","updated_at":"2025-09-21T22:31:20.192Z","avatar_url":"https://github.com/isap-ou.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Enum Helpers\n\nThis package brings some helpers to native [PHP Enums](https://www.php.net/manual/en/language.enumerations.basics.php)\n\n![Laravel Enum Helpers](https://github.com/isap-ou/laravel-enum-helpers/blob/main/images/banner.jpg?raw=true)\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/isap-ou/laravel-enum-helpers.svg?style=flat-square)](https://packagist.org/packages/isap-ou/laravel-enum-helpers)\n[![Total Downloads](https://img.shields.io/packagist/dt/isap-ou/laravel-enum-helpers.svg?style=flat-square)](https://packagist.org/packages/isap-ou/laravel-enum-helpers)\n\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require isap-ou/laravel-enum-helpers\n```\n\nYou will most likely need to edit the extensive configuration, so you can publish the config file with:\n\n```bash\nphp artisan vendor:publish --tag=\"enum-helpers-config\"\n```\n\nDefault config\n\n```php\nreturn [\n    'enum_locations' =\u003e [\n        'app/Enums' =\u003e '\\\\App\\\\Enums\\\\',\n    ],\n\n    'label' =\u003e [\n        'prefix' =\u003e null,\n        'namespace' =\u003e null,\n    ],\n\n    'post_migrate' =\u003e true,\n\n    'js_objects_file' =\u003e 'resources/js/enums.js',\n];\n```\n\n1. `enum_locations` - path where enums located. Key is directory with enums, value - namespace for specified directory\n2. `post_migrate` - enable or disable post migrate event listener\n3. `js_objects_file` - path for generated js output\n4. `label.prefix` - get default prefix for translations of enum fields\n5. `label.namespace` - get default prefix for translations of enum namespace (when using as part of own package)\n\n## Available helpers\n### Migration helper\n\nThe way easy to add all enums to database column. \n\nJust add to Enum trait `InteractWithCollection`\n```php\nuse IsapOu\\EnumHelpers\\Concerns\\InteractWithCollection;\n\nenum ExampleEnum: string\n{\n\n    use InteractWithCollection;\n\n    case ENUM_ONE = 'enum_one';\n    case ENUM_TWO = 'enum_two';\n}\n```\n\nAnd in migration class\n\n```php\nuse Illuminate\\Database\\Migrations\\Migration;\nuse Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Support\\Facades\\Schema;\n\nreturn new class extends Migration\n{\n    /**\n     * Run the migrations.\n     */\n    public function up(): void\n    {\n        Schema::create('table_name', function (Blueprint $table){\n            ...\n            $table-\u003eenum('enum_column', ExampleEnum::values()-\u003etoArray());\n            ...\n        });\n    }\n}\n```\n\n### Update enum columns in DB\n\nArtisan command allows update available(possible) values for specific enum column.\n\nModify enum:\n\n```php\nuse IsapOu\\EnumHelpers\\Contracts\\UpdatableEnumColumns;\n\nenum ExampleEnum: string implements UpdatableEnumColumns\n{\n\n    case ENUM_ONE = 'enum_one';\n    case ENUM_TWO = 'enum_two';\n    \n    public static function tables(): array\n    {\n        return [\n            'table_name' =\u003e 'enum_column'\n        ];\n    }\n}\n```\n\nAnd run command\n\n```bash\nphp artisan enum-helpers:migrate:enums\n```\n\nThere is also a listener enabled by default that will run after a successful migration. \nTo disable it edit `enum-helpers.php`: \n\n```php\n\u003c?php \nreturn [\n    ...\n    \n    'post_migrate' =\u003e false,\n    \n    ...\n]\n```\n\n\n### Convert PHP Enums to JS objects\n\nArtisan command allows generate js objects based on enums\n\n\nModify enum:\n\n```php\nuse IsapOu\\EnumHelpers\\Contracts\\UpdatableEnumColumns;\n\nenum ExampleEnum: string implements JsConvertibleEnum\n{\n    case ENUM_ONE = 'enum_one';\n    case ENUM_TWO = 'enum_two';\n}\n```\nAnd run command\n\n```bash\nphp artisan enum-helpers:js:export\n```\n\nOutput will\n\n```js\nexport const ExampleEnum = Object.freeze({ENUM_ONE: 'enum_one', ENUM_TWO: 'enum_two'})\n```\n\nYou can specify output path in config `enum-helpers.php`\n\n```php\nreturn [\n    ...\n    \n    'js_objects_file' =\u003e 'resources/js/enums.js'\n    \n    ...\n]\n```\n\n### Label Helper\n\nThe Label helper allows you to transform an enum instance into a textual label, \nmaking it useful for displaying human-readable, translatable enum values in your UI.\n\n```php\nuse IsapOu\\EnumHelpers\\Concerns\\HasLabel;\n\nenum ExampleEnum: string\n{\n    use HasLabel;\n\n    case ENUM_ONE = 'enum_one';\n    case ENUM_TWO = 'enum_two';\n}\n```\n\nYou can retrieve a textual label for an enum case using the getLabel method:\n\n```php\nExampleEnum::ENUM_ONE-\u003egetLabel()\n```\n\nBy default, the getLabel method attempts to find a translation key, following this format: `ExampleEnum.ENUM_ONE`.\n    1.\t`ExampleEnum` - The class name of the enum.\n    2.\t`ENUM_ONE` - The enum case name.\n\n#### Parameters\n\nThe getLabel method accepts three optional parameters:\n\t1.\t`prefix`: Prepends a prefix to the translation key.\n\t2.\t`namespace`: Prepends a namespace to the translation key. This is particularly useful when developing packages.\n\t3.\t`locale`: Allows you to specify the locale for translation. If not provided, the app’s default locale will be used.\n\n##### Example with custom parameters:\n```php\nExampleEnum::ENUM_ONE-\u003egetLabel('custom_prefix', 'custom_namespace', 'fr');\n```\nThis will retrieve the French (fr) translation with the specified prefix and namespace.\n\n#### getLabels Method\n\nThe getLabels method returns a collection of labels for all enum cases, \nmaking it convenient to retrieve or display translatable labels for multiple enum values at once.\n\n```php\n$labels = ExampleEnum::getLabels();\n\n// Output:\n// Illuminate\\Support\\Collection {#1234\n//     all: [\n//         \"ENUM_ONE\" =\u003e \"Enum One Label\",\n//         \"ENUM_TWO\" =\u003e \"Enum Two Label\",\n//     ],\n// }\n```\n\n##### Customizing Prefix, Namespace, and Locale\n\nYou can customize the prefix, namespace, and locale for the translations when retrieving labels for all cases:\n```php\n$customLabels = ExampleEnum::getLabels('custom_prefix', 'custom_namespace', 'fr');\n```\n\n#### Global Configuration for Prefix and Namespace\nYou can define the `prefix` and `namespace` globally in the configuration file `enum-helpers.config`, \nor override them on the enum level by defining the following methods:\n\n```php\nprotected function getPrefix(): ?string\n{\n    return 'prefix';\n}\n\nprotected function getNamespace(): ?string\n{\n    return 'namespace';\n}\n```\n\nThe global or per-enum configurations will be used unless you provide custom values when calling getLabel or getLabels.\n\n\u003e **Optional**. Interface `\\IsapOu\\EnumHelpers\\Contracts\\HasLabel` for better IDE support\n\nFor better IDE support, you can implement the \\IsapOu\\EnumHelpers\\Contracts\\HasLabel interface. \nThis helps provide autocomplete suggestions and improves code hinting for the getLabel method when working with enums.\n\n##### FilamentPHP Compatibility\nThis helper is fully compatible with [Enums in FilamentPHP](https://filamentphp.com/docs/3.x/support/enums)\n\n```php\n\nuse Filament\\Support\\Contracts\\HasLabel;\n\nenum ExampleEnum: string implements HasLabel\n{\n    use HasLabel;\n\n    case ENUM_ONE = 'enum_one';\n    case ENUM_TWO = 'enum_two';\n}\n```\n\n## Contributing\n\nPlease, submit bugs or feature requests via the [Github issues](https://github.com/isap-ou/laravel-enum-helpers/issues).\n\nPull requests are welcomed!\n\nThanks!\n\n## License\n\nThis project is open-sourced software licensed under the [MIT License](https://opensource.org/licenses/MIT).\n\nYou are free to use, modify, and distribute it in your projects, as long as you comply with the terms of the license.\n\n---\n\nMaintained by [ISAPP](https://isapp.be) and [ISAP OÜ](https://isap.me).  \nCheck out our software development services at [isap.me](https://isap.me).\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fisap-ou%2Flaravel-enum-helpers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fisap-ou%2Flaravel-enum-helpers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fisap-ou%2Flaravel-enum-helpers/lists"}