{"id":19818990,"url":"https://github.com/ekvedaras/laravel-enum","last_synced_at":"2026-05-11T01:16:45.594Z","repository":{"id":56976226,"uuid":"291488449","full_name":"ekvedaras/laravel-enum","owner":"ekvedaras","description":"🔠  Laravel integration of ekvedaras/php-enum package","archived":false,"fork":false,"pushed_at":"2021-09-20T09:07:01.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-11T08:14:43.851Z","etag":null,"topics":["casting","enum","laravel","laravel-enum","php","php-enum"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ekvedaras.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG-1.x.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-30T14:40:05.000Z","updated_at":"2021-09-20T09:07:04.000Z","dependencies_parsed_at":"2022-08-21T11:20:32.717Z","dependency_job_id":null,"html_url":"https://github.com/ekvedaras/laravel-enum","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekvedaras%2Flaravel-enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekvedaras%2Flaravel-enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekvedaras%2Flaravel-enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekvedaras%2Flaravel-enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ekvedaras","download_url":"https://codeload.github.com/ekvedaras/laravel-enum/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241175290,"owners_count":19922476,"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","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":["casting","enum","laravel","laravel-enum","php","php-enum"],"created_at":"2024-11-12T10:17:30.279Z","updated_at":"2026-05-11T01:16:45.549Z","avatar_url":"https://github.com/ekvedaras.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Enum\n\n![Tests](https://github.com/ekvedaras/laravel-enum/workflows/run-tests/badge.svg)\n[![Code Coverage](https://img.shields.io/codecov/c/gh/ekvedaras/laravel-enum/main?style=flat)](https://app.codecov.io/gh/ekvedaras/laravel-enum)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](LICENSE)\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/ekvedaras/laravel-enum.svg?style=flat)](https://packagist.org/packages/ekvedaras/laravel-enum)\n[![Total Downloads](https://img.shields.io/packagist/dt/ekvedaras/laravel-enum.svg?style=flat)](https://packagist.org/packages/ekvedaras/laravel-enum)\n\n\u003cimg src=\"logo.svg\" width=\"192\" height=\"192\"/\u003e\n\n![Twitter Follow](https://img.shields.io/twitter/follow/ekvedaras?style=plastic)\n\n\nThis package integrates [ekvedaras/php-enum](https://github.com/ekvedaras/php-enum)\ninto Laravel by providing [enum value casting in models](https://laravel.com/docs/7.x/eloquent-mutators#custom-casts) which was introduced in Laravel 7.\n\n## Usage\n\n**PaymentStatus.php**\n```php\nuse EKvedaras\\LaravelEnum\\Enum;\n\nclass PaymentStatus extends Enum\n{\n    /**\n     * @return static\n     */\n    final public static function pending(): self\n    {\n        return static::get('pending', 'Payment is pending');\n    }\n\n    /**\n     * @return static\n     */\n    final public static function completed(): self\n    {\n        return static::get('completed', 'Payment has been processed');\n    }\n\n    /**\n     * @return static\n     */\n    final public static function failed(): self\n    {\n        return static::get('failed', 'Payment has failed');\n    }\n}\n```\n\n### Casting\n\n**Payment.php**\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass Payment extends Model\n{\n    protected $casts = [\n        'status' =\u003e PaymentStatus::class,\n    ];\n}\n```\n\nSetting and retrieving status:\n```php\n$payment = new Payment();\n\n// It is advised to always set enum objects instead of strings for better usage analysis\n$payment-\u003estatus = PaymentStatus::pending();\n// However, above works the same as this\n$payment-\u003estatus = 'pending';\n// or this\n$payment-\u003estatus = PaymentStatus::pending()-\u003eid();\n\ndump($payment-\u003estatus === PaymentStatus::pending()); // true\n\n$payment-\u003estatus = 'invalid'; // throws OutOfBoundsException\n```\n\n### Validation\n\nA built in `in` validator can be used.\n\n```php\nuse Illuminate\\Validation\\Rule;\n\n$rules = [\n    'status' =\u003e Rule::in(PaymentStatus::keys())\n];\n\n// or\n\n$rules = [\n    'status' =\u003e 'in:' . PaymentStatus::keyString(),\n];\n```\n\n## Changelog\n\nSee changes in changelog files:\n\n* [v1 changelog](CHANGELOG-1.x.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fekvedaras%2Flaravel-enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fekvedaras%2Flaravel-enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fekvedaras%2Flaravel-enum/lists"}