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.11.0(2mo ago)632.2M↓24.4%9[2 issues](https://github.com/artkonekt/enum-eloquent/issues)11MITPHPPHP ^8.2CI failing

Since Oct 6Pushed 2mo 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 3d ago

READMEChangelogDependencies (4)Versions (25)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 - 13

[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

67

—

FairBetter than 99% of packages

Maintenance84

Actively maintained with recent releases

Popularity54

Moderate usage in the ecosystem

Community29

Small or concentrated contributor base

Maturity85

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 81.9% 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 ~135 days

Recently: every ~234 days

Total

24

Last Release

78d ago

PHP version history (5 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

1.11.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5520702?v=4)[konekt](/maintainers/konekt)[@konekt](https://github.com/konekt)

---

Top Contributors

[![fulopattila122](https://avatars.githubusercontent.com/u/1162360?v=4)](https://github.com/fulopattila122 "fulopattila122 (113 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

###  Code Quality

TestsPHPUnit

### 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

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M96](/packages/mongodb-laravel-mongodb)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k32.6M46](/packages/kirschbaum-development-eloquent-power-joins)[watson/validating

Eloquent model validating trait.

9803.5M54](/packages/watson-validating)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k332.0k1](/packages/cybercog-laravel-love)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.3M18](/packages/reedware-laravel-relation-joins)

PHPackages © 2026

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