Skip to content

Easy way to get the possible values in a ENUM field.

Notifications You must be signed in to change notification settings

erusso7/EnumerableTrait

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

EnumerableTrait

Easy way to get the possible values in a ENUM field.

#Usage:

Create a table with a ENUM type column:

Table: Users
+----------------+--------------------------------------+------+-----+---------+----------------+
| Field          | Type                                 | Null | Key | Default | Extra          |
+----------------+--------------------------------------+------+-----+---------+----------------+
| id             | int(10) unsigned                     | NO   | PRI | NULL    | auto_increment |
| status         | enum('banned','active','validating') | NO   |     | NULL    |                |
+----------------+--------------------------------------+------+-----+---------+----------------+

Create a class to use the trait:

Note: If you don't create the property $table, you should call it with the second argument.

<?php

namespace App\Models;

class User 
{
    use EnumerableTrait;
    
    protected $table = 'users';
}

Call statically the method

var_export(User::getEnumValues('status'));

Output:
array (
  0 => 'banned',
  1 => 'active',
  2 => 'validating',
)

About

Easy way to get the possible values in a ENUM field.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages