1. Go to this page and download the library: Download mindtwo/laravel-enum library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mindtwo / laravel-enum example snippets
'BenSampo\Enum\EnumServiceProvider'
php artisan make:enum UserType
namespace App\Enums;
use BenSampo\Enum\Enum;
final class UserType extends Enum
{
const Administrator = 0;
const Moderator = 1;
const Subscriber = 2;
const SuperAdministrator = 3;
}
UserType::Administrator // Has a value of 0
// Standard new PHP class, passing the desired enum value as a parameter
$enumInstance = new UserType(UserType::Administrator);
// Static getInstance method, again passing the desired enum value as a parameter
$enumInstance = UserType::getInstance(UserType::Administrator);
// Statically calling the key name as a method, utilizing __callStatic magic
$enumInstance = UserType::Administrator();
// Using the coerce static method to attempt to instantiate an Enum using the given value if it exists.
$enumInstance = UserType::coerce($someValue);
use BenSampo\Enum\Traits\CastsEnums;
use BenSampo\Enum\Tests\Enums\UserType;
use Illuminate\Database\Eloquent\Model;
class Example extends Model
{
use CastsEnums;
protected $enumCasts = [
// 'attribute_name' => Enum::class
'user_type' => UserType::class,
];
/**
* Existing casts are processed before $enumCasts which can be useful if you're
* taking input from forms and your enum values are integers.
*/
protected $casts = [
'user_type' => 'int',
];
}
$example = Example::first();
$example->user_type // Instance of UserType
$example = Example::first();
// Set using enum value
$example->user_type = UserType::Moderator;
// Set using enum instance
$example->user_type = UserType::Moderator();
public function store(Request $request)
{
$this->validate($request, [
'user_type' => ['
new EnumValue(UserType::class, false) // Turn off strict type checking.
public function store(Request $request)
{
$this->validate($request, [
'user_type' => ['
var_dump(UserType::getInstances());
array(4) {
'Administrator' =>
class BenSampo\Enum\Tests\Enums\UserType#415 (3) {
public $key =>
string(13) "Administrator"
public $value =>
int(0)
public $description =>
string(13) "Administrator"
}
'Moderator' =>
class BenSampo\Enum\Tests\Enums\UserType#396 (3) {
public $key =>
string(9) "Moderator"
public $value =>
int(1)
public $description =>
string(9) "Moderator"
}
'Subscriber' =>
class BenSampo\Enum\Tests\Enums\UserType#393 (3) {
public $key =>
string(10) "Subscriber"
public $value =>
int(2)
public $description =>
string(10) "Subscriber"
}
'SuperAdministrator' =>
class BenSampo\Enum\Tests\Enums\UserType#102 (3) {
public $key =>
string(18) "SuperAdministrator"
public $value =>
int(3)
public $description =>
string(19) "Super administrator"
}
}
UserType::coerce(0); // Returns instance of UserType with the value set to UserType::Administrator
UserType::coerce(99); // Returns null (not a valid enum value)