Use most popular enum implementation for PHP
myclabs/php-enum as Eloquent model attributes.
composer require kminek/laravel-enum
Instead of using base enum class from myclabs/php-enum
:
use \MyCLabs\Enum\Enum;
class Brand extends Enum
{
public const TOYOTA = 'toyota';
public const BMW = 'bmw';
public const PEUGEOT = 'peugeot';
}
use enum class from this package:
use \Kminek\LaravelEnum\Enum;
class Brand extends Enum
{
public const TOYOTA = 'toyota';
public const BMW = 'bmw';
public const PEUGEOT = 'peugeot';
}
Setup model attribute to use enum class:
class Car extends \Illuminate\Database\Eloquent\Model
{
protected $casts = [
'brand' => Brand::class,
];
}
If you would like to allow null
values also:
class Car extends \Illuminate\Database\Eloquent\Model
{
protected $casts = [
'brand' => Brand::class.':nullable',
];
}
From now on you can get/set enums:
// set
$car = new Car();
$car->brand = new Brand(Brand::BMW);
// or
$car->brand = Brand::BMW;
// get
$brand = $car->brand; // $brand is enum instance
composer test
composer cs