{"id":20681998,"url":"https://github.com/artkonekt/enum-eloquent","last_synced_at":"2025-04-07T06:04:00.520Z","repository":{"id":38549973,"uuid":"105900484","full_name":"artkonekt/enum-eloquent","owner":"artkonekt","description":"Enum attribute casting for Eloquent models","archived":false,"fork":false,"pushed_at":"2025-03-03T11:35:00.000Z","size":108,"stargazers_count":62,"open_issues_count":3,"forks_count":7,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-31T05:02:00.159Z","etag":null,"topics":["eloquent-models","enum","laravel","laravel5-package"],"latest_commit_sha":null,"homepage":null,"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/artkonekt.png","metadata":{"files":{"readme":"README.md","changelog":"Changelog.md","contributing":null,"funding":null,"license":"LICENSE","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":"2017-10-05T14:32:33.000Z","updated_at":"2025-03-03T11:35:03.000Z","dependencies_parsed_at":"2022-08-21T13:10:47.192Z","dependency_job_id":"275676dc-2414-4d8b-a333-1be3e8db6086","html_url":"https://github.com/artkonekt/enum-eloquent","commit_stats":{"total_commits":114,"total_committers":8,"mean_commits":14.25,"dds":0.368421052631579,"last_synced_commit":"b3d4ec41f020ed1d2f9059c88604cd6a104ae67b"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artkonekt%2Fenum-eloquent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artkonekt%2Fenum-eloquent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artkonekt%2Fenum-eloquent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artkonekt%2Fenum-eloquent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/artkonekt","download_url":"https://codeload.github.com/artkonekt/enum-eloquent/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247601447,"owners_count":20964864,"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":["eloquent-models","enum","laravel","laravel5-package"],"created_at":"2024-11-16T22:12:28.769Z","updated_at":"2025-04-07T06:04:00.497Z","avatar_url":"https://github.com/artkonekt.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Konekt Enum Eloquent Bindings\n\n\n[![Tests](https://img.shields.io/github/actions/workflow/status/artkonekt/enum-eloquent/tests.yml?branch=master\u0026style=flat-square)](https://github.com/artkonekt/enum-eloquent/actions?query=workflow%3Atests)\n[![Packagist Stable Version](https://img.shields.io/packagist/v/konekt/enum-eloquent.svg?style=flat-square\u0026label=stable)](https://packagist.org/packages/konekt/enum-eloquent)\n[![Packagist downloads](https://img.shields.io/packagist/dt/konekt/enum-eloquent.svg?style=flat-square)](https://packagist.org/packages/konekt/enum-eloquent)\n[![StyleCI](https://styleci.io/repos/105900484/shield?branch=master)](https://styleci.io/repos/105900484)\n[![MIT Software License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE)\n\nThis package provides support for auto casting [konekt enum](https://konekt.dev/enum) fields in [Eloquent models](https://laravel.com/docs/9.x/eloquent-mutators).\n\n\u003e Supported Konekt Enum versions are 2.x, 3.x and 4.x with Eloquent (Laravel) 8 - 12\n\n[Changelog](Changelog.md)\n\n## Installation\n\n`composer require konekt/enum-eloquent`\n\n## Usage\n\n1. Add the `CastsEnums` trait to your model\n2. Define the attributes to be casted via the `protected $enums` property on the model\n\n### Example\n\n**The Enum:**\n\n```php\nnamespace App;\n\nuse Konekt\\Enum\\Enum;\n\nclass OrderStatus extends Enum\n{\n    const __DEFAULT = self::PENDING; \n    // const __default = self::PENDING; // usage of default in v2.x \n\n    const PENDING   = 'pending';\n    const CANCELLED = 'cancelled';\n    const COMPLETED = 'completed';\n\n}\n```\n\n**The Model:**\n\n```php\nnamespace App;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Konekt\\Enum\\Eloquent\\CastsEnums;\n\nclass Order extends Model\n{\n    use CastsEnums;\n\n    protected $enums = [\n        'status' =\u003e OrderStatus::class\n    ];\n}\n```\n\n**Client code:**\n```php\n$order = Order::create([\n    'status' =\u003e 'pending'\n]);\n\n// The status attribute will be an enum object:\necho get_class($order-\u003estatus);\n// output: App\\OrderStatus\n\necho $order-\u003estatus-\u003evalue();\n// output: 'pending'\n\necho $order-\u003estatus-\u003eisPending() ? 'yes' : 'no';\n// output: yes\n\necho $order-\u003estatus-\u003eisCancelled() ? 'yes' : 'no';\n// output: no\n\n// You can assign an enum object as attribute value:\n$order-\u003estatus = OrderStatus::COMPLETED();\necho $order-\u003estatus-\u003evalue();\n// output: 'completed'\n\n// It also works with mass assignment:\n$order = Order::create([\n    'status' =\u003e OrderStatus::COMPLETED()    \n]);\n\necho $order-\u003estatus-\u003evalue();\n// output 'completed'\n\n// It still accepts scalar values:\n$order-\u003estatus = 'completed';\necho $order-\u003estatus-\u003eisCompleted() ? 'yes' : 'no';\n// output: yes\n\n// But it doesn't accept scalar values that aren't in the enum:\n$order-\u003estatus = 'negotiating';\n// throws UnexpectedValueException\n// Given value (negotiating) is not in enum `App\\OrderStatus`\n```\n\n### Resolving Enum Class Runtime\n\nIt is possible to defer the resolution of an Enum class to runtime.\n\nIt happens using the `ClassName@method` notation known from Laravel.\n\nThis is useful for libraries, so you can 'late-bind' the actual enum class and let the user to extend it.\n\n#### Example\n\n**The Model:**\n\n```php\nnamespace App;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Konekt\\Enum\\Eloquent\\CastsEnums;\n\nclass Order extends Model\n{\n    use CastsEnums;\n\n    protected $enums = [\n        'status' =\u003e 'OrderStatusResolver@enumClass'\n    ];\n}\n```\n\n**The Resolver:**\n\n```php\nnamespace App;\n\nclass OrderStatusResolver\n{\n    /**\n     * Returns the enum class to use as order status enum\n     *\n     * @return string\n     */\n    public static function enumClass()\n    {\n        return config('app.order.status.class', OrderStatus::class);\n    }\n}\n```\n\nThis way the enum class becomes configurable without the need to modify the Model code.\n\n## Laravel Collective Forms Compatibility\n\nLaravel Collective [Forms Package](https://laravelcollective.com/docs/master/html) provides the\n`Form` facade known from Laravel v4.x.\n\nIn case you want to use the Forms package with this one, you need to add the\n`EnumsAreCompatibleWithLaravelForms` trait to your model, next to `CastsEnums`.\n\nThis will fix a problem where the forms package detects the enum label instead of its actual value\nas the value of the field.\n\nIt is being done by adding the (undocumented) `getFormValue()` method to the model, that is being\nused by the forms library to obtain form field value.\n\n---\n\nEnjoy!\n\nFor detailed usage of konekt enums refer to the [Konekt Enum Documentation](https://konekt.dev/enum).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartkonekt%2Fenum-eloquent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fartkonekt%2Fenum-eloquent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartkonekt%2Fenum-eloquent/lists"}