PHPackages                             konekt/enum-eloquent - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. konekt/enum-eloquent

ActiveLibrary[Database &amp; ORM](/categories/database)

konekt/enum-eloquent
====================

Enum attribute casting for Eloquent models

1.10.0(1y ago)622.0M—7.8%9[2 issues](https://github.com/artkonekt/enum-eloquent/issues)11MITPHPPHP ^8.0CI passing

Since Oct 6Pushed 1y ago5 watchersCompare

[ Source](https://github.com/artkonekt/enum-eloquent)[ Packagist](https://packagist.org/packages/konekt/enum-eloquent)[ RSS](/packages/konekt-enum-eloquent/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (24)Used By (11)

Konekt Enum Eloquent Bindings
=============================

[](#konekt-enum-eloquent-bindings)

[![Tests](https://camo.githubusercontent.com/a2d15ddee66ac98aec8bed1fe21eaf02029ac8cbba4677898733c45250ea513b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6172746b6f6e656b742f656e756d2d656c6f7175656e742f74657374732e796d6c3f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://github.com/artkonekt/enum-eloquent/actions?query=workflow%3Atests)[![Packagist Stable Version](https://camo.githubusercontent.com/497403b2510a7bdde9adac9f0bc6260500b8e0c0008cbf472a45757cc2ea8dce/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b6f6e656b742f656e756d2d656c6f7175656e742e7376673f7374796c653d666c61742d737175617265266c6162656c3d737461626c65)](https://packagist.org/packages/konekt/enum-eloquent)[![Packagist downloads](https://camo.githubusercontent.com/bef83715075cb3f93f44913bf4827f364458a74c4dd21e58a4b9f075ffc38143/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6b6f6e656b742f656e756d2d656c6f7175656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/konekt/enum-eloquent)[![StyleCI](https://camo.githubusercontent.com/7f81691f1991daff99830bd9454de72e8eb1046a69abd3e1d9b10c267f53b6d5/68747470733a2f2f7374796c6563692e696f2f7265706f732f3130353930303438342f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/105900484)[![MIT Software License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](LICENSE)

This package provides support for auto casting [konekt enum](https://konekt.dev/enum) fields in [Eloquent models](https://laravel.com/docs/9.x/eloquent-mutators).

> Supported Konekt Enum versions are 2.x, 3.x and 4.x with Eloquent (Laravel) 8 - 12

[Changelog](Changelog.md)

Installation
------------

[](#installation)

`composer require konekt/enum-eloquent`

Usage
-----

[](#usage)

1. Add the `CastsEnums` trait to your model
2. Define the attributes to be casted via the `protected $enums` property on the model

### Example

[](#example)

**The Enum:**

```
namespace App;

use Konekt\Enum\Enum;

class OrderStatus extends Enum
{
    const __DEFAULT = self::PENDING;
    // const __default = self::PENDING; // usage of default in v2.x

    const PENDING   = 'pending';
    const CANCELLED = 'cancelled';
    const COMPLETED = 'completed';

}
```

**The Model:**

```
namespace App;

use Illuminate\Database\Eloquent\Model;
use Konekt\Enum\Eloquent\CastsEnums;

class Order extends Model
{
    use CastsEnums;

    protected $enums = [
        'status' => OrderStatus::class
    ];
}
```

**Client code:**

```
$order = Order::create([
    'status' => 'pending'
]);

// The status attribute will be an enum object:
echo get_class($order->status);
// output: App\OrderStatus

echo $order->status->value();
// output: 'pending'

echo $order->status->isPending() ? 'yes' : 'no';
// output: yes

echo $order->status->isCancelled() ? 'yes' : 'no';
// output: no

// You can assign an enum object as attribute value:
$order->status = OrderStatus::COMPLETED();
echo $order->status->value();
// output: 'completed'

// It also works with mass assignment:
$order = Order::create([
    'status' => OrderStatus::COMPLETED()
]);

echo $order->status->value();
// output 'completed'

// It still accepts scalar values:
$order->status = 'completed';
echo $order->status->isCompleted() ? 'yes' : 'no';
// output: yes

// But it doesn't accept scalar values that aren't in the enum:
$order->status = 'negotiating';
// throws UnexpectedValueException
// Given value (negotiating) is not in enum `App\OrderStatus`
```

### Resolving Enum Class Runtime

[](#resolving-enum-class-runtime)

It is possible to defer the resolution of an Enum class to runtime.

It happens using the `ClassName@method` notation known from Laravel.

This is useful for libraries, so you can 'late-bind' the actual enum class and let the user to extend it.

#### Example

[](#example-1)

**The Model:**

```
namespace App;

use Illuminate\Database\Eloquent\Model;
use Konekt\Enum\Eloquent\CastsEnums;

class Order extends Model
{
    use CastsEnums;

    protected $enums = [
        'status' => 'OrderStatusResolver@enumClass'
    ];
}
```

**The Resolver:**

```
namespace App;

class OrderStatusResolver
{
    /**
     * Returns the enum class to use as order status enum
     *
     * @return string
     */
    public static function enumClass()
    {
        return config('app.order.status.class', OrderStatus::class);
    }
}
```

This way the enum class becomes configurable without the need to modify the Model code.

Laravel Collective Forms Compatibility
--------------------------------------

[](#laravel-collective-forms-compatibility)

Laravel Collective [Forms Package](https://laravelcollective.com/docs/master/html) provides the `Form` facade known from Laravel v4.x.

In case you want to use the Forms package with this one, you need to add the `EnumsAreCompatibleWithLaravelForms` trait to your model, next to `CastsEnums`.

This will fix a problem where the forms package detects the enum label instead of its actual value as the value of the field.

It is being done by adding the (undocumented) `getFormValue()` method to the model, that is being used by the forms library to obtain form field value.

---

Enjoy!

For detailed usage of konekt enums refer to the [Konekt Enum Documentation](https://konekt.dev/enum).

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance43

Moderate activity, may be stable

Popularity54

Moderate usage in the ecosystem

Community27

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 81.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~123 days

Recently: every ~186 days

Total

23

Last Release

441d ago

PHP version history (4 changes)1.0.0PHP &gt;=7.0.0

1.3.0PHP ^7.1.3

1.7.0PHP ^7.3 | ^8.0

1.7.1PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/9c398dd02c93ecf6aa344f367f5744aeb32b4c7bbc23b1b22e95336f45bf0d5a?d=identicon)[konekt](/maintainers/konekt)

---

Top Contributors

[![fulopattila122](https://avatars.githubusercontent.com/u/1162360?v=4)](https://github.com/fulopattila122 "fulopattila122 (110 commits)")[![TheM1984](https://avatars.githubusercontent.com/u/13640063?v=4)](https://github.com/TheM1984 "TheM1984 (18 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (3 commits)")[![litvinjuan](https://avatars.githubusercontent.com/u/42967171?v=4)](https://github.com/litvinjuan "litvinjuan (2 commits)")[![masroore](https://avatars.githubusercontent.com/u/200963?v=4)](https://github.com/masroore "masroore (1 commits)")[![semyonchetvertnyh](https://avatars.githubusercontent.com/u/2598761?v=4)](https://github.com/semyonchetvertnyh "semyonchetvertnyh (1 commits)")

---

Tags

eloquent-modelsenumlaravellaravel5-packagelaravelenumeloquentkonektartkonekt

### Embed Badge

![Health badge](/badges/konekt-enum-eloquent/health.svg)

```
[![Health](https://phpackages.com/badges/konekt-enum-eloquent/health.svg)](https://phpackages.com/packages/konekt-enum-eloquent)
```

###  Alternatives

[henzeb/enumhancer

Your framework-agnostic Swiss Army knife for PHP 8.1+ native enums

69287.4k2](/packages/henzeb-enumhancer)[rtconner/laravel-likeable

Trait for Laravel Eloquent models to allow easy implementation of a 'like' or 'favorite' or 'remember' feature.

394388.0k5](/packages/rtconner-laravel-likeable)[highsolutions/eloquent-sequence

A Laravel package for easy creation and management sequence support for Eloquent models with elastic configuration.

121130.3k](/packages/highsolutions-eloquent-sequence)[konekt/acl

Concord Module for Permission handling (Laravel 10 - 12)

1070.8k1](/packages/konekt-acl)[cybercog/laravel-nova-ban

A Laravel Nova banning functionality for your application.

40199.8k](/packages/cybercog-laravel-nova-ban)[phaza/single-table-inheritance

Single Table Inheritance Trait

1515.8k](/packages/phaza-single-table-inheritance)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
