Laravel Enum Collections
Save a collection of Enums in an Eloquent field and interact with it.
Compatible with PureEnum
, BackedEnum
and datomatic/laravel-enum-helper
package.
Installation
You can install the package via composer:
composer require datomatic/laravel-enum-collections
Set up
Before you can use this package you must setup the eloquent Model.
1. Database Migration
Schema::table('table', function (Blueprint $table) {
$table->json('field_name')->nullable()->after('some_field');
});
2. Model set up
To set up your model you must:
- add a custom cast
EnumCollections::class
- add an
$enumCollections
array containing the fields and relative enum classes - add an optional
HasEnumCollections
trait to make query on enum collections fields
You can also set more than one field if you need.
use Datomatic\EnumCollections\Casts\EnumCollections;
use Datomatic\EnumCollections\EnumCollection;
use Illuminate\Database\Eloquent\Model;
class TestModel extends Model
{
use HasEnumCollections;
protected $casts = [
'field_name' => EnumCollections::class,
}
public array $enumCollections = [
'field_name' => FieldEnum::class,
];
}
Usage
After model set up you can use the package potentials.
Set the enum collection field
You can set enum collection field passing a single element, a collection or an array of elements.
After the setting, the field will become an EnumCollection
.
Each element can be an:
- enum object instance
- enum case name string
- enum case value (only for
BackedEnum
) - enum case (string) value (only for
IntBackedEnum
)
enum FieldEnum: int
{
case PRIVATE = 1;
case PUBLIC = 2;
case PROTECTED = 3;
}
$model = new TestModel();
$model->field_name = FieldEnum::PRIVATE; // ✅ EnumCollection<FieldEnum::PRIVATE>
$model->field_name = 'PRIVATE'; // ✅ EnumCollection<FieldEnum::PRIVATE>
$model->field_name = 1; // ✅ EnumCollection<FieldEnum::PRIVATE>
$model->field_name = '1'; // ✅ EnumCollection<FieldEnum::PRIVATE>
$model->field_name = [FieldEnum::PRIVATE,FieldEnum::PUBLIC]; // ✅ EnumCollection<FieldEnum>
$model->field_name = collect([FieldEnum::PRIVATE,FieldEnum::PUBLIC]); // ✅ EnumCollection<FieldEnum>
EnumCollection
The EnumCollection
extend the Laravel collection and overload the contains
method to add the compatibility with:
- enum object instance
- enum case name string
- enum case value (only for
BackedEnum
) - enum case (string) value (only for
IntBackedEnum
)
$model = new TestModel();
$model->field_name = [FieldEnum::PRIVATE,FieldEnum::PUBLIC];
$model->field_name->contains(FieldEnum::PRIVATE); // true
$model->field_name->contains(FieldEnum::PROTECTED); // false
$model->field_name->contains(1); // true
$model->field_name->contains('1'); // true
$model->field_name->contains('PRIVATE'); // true
HasEnumCollections trait
If you include also the HasEnumCollections
into the model you can query the models with the new where functions whereEnumCollectionContains
and orWhereEnumCollectionContains
.
TestModel::whereEnumCollectionContains('field_name', FieldEnum::PRIVATE)->get()
TestModel::whereEnumCollectionContains('field_name', FieldEnum::PRIVATE)
->whereEnumCollectionContains('field_name', FieldEnum::PUBLIC)
->get()
TestModel::whereEnumCollectionContains('field_name', FieldEnum::PRIVATE)
->orWhereEnumCollectionContains('field_name', FieldEnum::PUBLIC)
->get()
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.