Simple Enum cast for Eloquent ORM using myclabs/php-enum.

Overview

Enum cast for Eloquent

Latest Version on Packagist Total Downloads GitHub Actions

Simple Enum cast for Eloquent ORM using myclabs/php-enum.

Requirements

  • PHP 7.3 or higher
  • Laravel 8.0 or higher

Installation

You can install the package via composer:

composer require orkhanahmadov/eloquent-enum-cast

Usage

Let's say you have following Enum class using myclabs/php-enum.

namespace App\Enums;

use MyCLabs\Enum\Enum;

class Role extends Enum
{
    private const ADMIN = 1;
    private const USER = 2;
}

To make it Eloquent castable, instead of MyCLabs\Enum\Enum, extend from Orkhanahmadov\EloquentEnumCast\EnumCast

namespace App\Enums;

use Orkhanahmadov\EloquentEnumCast\EnumCast;

class Role extends EnumCast
{
    private const ADMIN = 1;
    private const USER = 2;
}

Note: Orkhanahmadov\EloquentEnumCast\EnumCast extends MyCLabs\Enum\Enum, you can still use any other methods and properties that it has.

Finally, in your Eloquent model cast attribute using your Enum class

use App\Enums\Role;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $casts = [
        'role' => Role::class,
    ];
}

Now Eloquent will cast raw database values to your App\Enums\Role enum whenever you retrieve User model from database.

Likewise, whenever you save User model with App\Enums\Role enum as role attribute Eloquent will automatically save enum's underlying value into database.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Builds Cycle ORM schemas from OpenAPI 3 component schemas

Phanua OpenAPI 3 + Jane + Cycle ORM = 🔥 Phanua builds Cycle ORM schemas from OpenAPI 3 component schemas. Released under the MIT License. WARNING: Th

Extra RedBean ORM

RedBeanPHP 5 RedBeanPHP is an easy to use ORM tool for PHP. Automatically creates tables and columns as you go No configuration, just fire and forget

Articulate - An alternative ORM for Laravel, making use of the data mapper pattern

Articulate Laravel: 8.* PHP: 8.* License: MIT Author: Ollie Read Author Homepage: https://ollie.codes Articulate is an alternative ORM for Laravel bas

MongoDB ORM that includes support for references,embed and multilevel inheritance.
MongoDB ORM that includes support for references,embed and multilevel inheritance.

Introduction Features Requirements Installation Setup Database Basic Usage - CRUD Relationship - Reference Relationship - Embed Collection Inheritance

 Low code , Zero Configuration ORM that creates models, config, database and tables on the fly.
Low code , Zero Configuration ORM that creates models, config, database and tables on the fly.

🚀 ARCA ORM 🔥 Low code , Zero Configuration ORM that creates models, config, database and tables on the fly. 🔥 🇮🇳 Made in India 🇮🇳 Complete docu

⚡️ Models like Eloquent for Elasticsearch.

Elasticsearch Eloquent 2.x This package allows you to interact with Elasticsearch as you interact with Eloquent models in Laravel. Requirements PHP =

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)

Laravel MongoDB This package adds functionalities to the Eloquent model and Query builder for MongoDB, using the original Laravel API. This library ex

Easily exclude model entities from eloquent queries
Easily exclude model entities from eloquent queries

Laravel Excludable Easily exclude model entities from eloquent queries. This package allows you to define a subset of model entities who should be exc

Eloquent Filter is a package for filter data of models by the query strings. Easy to use and fully dynamic.
Eloquent Filter is a package for filter data of models by the query strings. Easy to use and fully dynamic.

Eloquent Filter Eloquent Filter adds custom filters to your Eloquent Models in Laravel. It's easy to use and fully dynamic. Table of Content Introduct

Releases(1.4.0)
Owner
Orkhan Ahmadov
Software Engineer and Team Lead at @airlst. Everything PHP, Laravel, JavaScript, and Vue related.
Orkhan Ahmadov
Baum is an implementation of the Nested Set pattern for Laravel's Eloquent ORM.

Baum Baum is an implementation of the Nested Set pattern for Laravel 5's Eloquent ORM. For Laravel 4.2.x compatibility, check the 1.0.x branch branch

Estanislau Trepat 2.2k Jan 3, 2023
Extensions for the Eloquent ORM

Sofa/Eloquence Easy and flexible extensions for the Eloquent ORM. Currently available extensions: Searchable query - crazy-simple fulltext search thro

Jarek Tkaczyk 1.1k Dec 20, 2022
The Enobrev\ORM library is a small framework of classes meant to be used for simply mapping a mysql database to PHP classes, and for creating simply SQL statements using those classes.

The Enobrev\ORM library is a small framework of classes meant to be used for simply mapping a mysql database to PHP classes, and for creating simply SQL statements using those classes.

Mark Armendariz 0 Jan 7, 2022
Orm is a simple database abstraction layer that supports postgresql.

Orm What is it Orm is a simple database abstraction layer that supports postgresql. Welcome to join us or star us for encouragement. Requires php 8.1

null 2 Sep 28, 2022
Propel2 is an open-source high-performance Object-Relational Mapping (ORM) for modern PHP

Propel2 Propel2 is an open-source Object-Relational Mapping (ORM) for PHP. Requirements Propel uses the following Symfony Components: Config Console F

Propel 1.2k Dec 27, 2022
PHP DataMapper, ORM

Cycle ORM Cycle is PHP DataMapper, ORM and Data Modelling engine designed to safely work in classic and daemonized PHP applications (like RoadRunner).

Cycle ORM 1.1k Jan 8, 2023
Ouzo Framework - PHP MVC ORM

Ouzo is a PHP MVC framework with built-in ORM and util libraries. PHP 8.0 or later is required. We believe in clean code and simplicity. We value unit

Ouzo 69 Dec 27, 2022
Doctrine Object Relational Mapper (ORM)

3.0.x 2.9.x 2.8.x Doctrine 2 is an object-relational mapper (ORM) for PHP 7.1+ that provides transparent persistence for PHP objects. It sits on top o

Doctrine 9.5k Jan 2, 2023
ORM layer that creates models, config and database on the fly

RedBeanPHP 5 RedBeanPHP is an easy to use ORM tool for PHP. Automatically creates tables and columns as you go No configuration, just fire and forget

Gabor de Mooij 2.2k Jan 9, 2023
A drop-in Doctrine ORM 2 implementation for Laravel 5+ and Lumen

Laravel Doctrine ORM A drop-in Doctrine ORM 2 implementation for Laravel 5+ $scientist = new Scientist( 'Albert', 'Einstein' ); $scientist->a

Laravel Doctrine 777 Dec 17, 2022