This package provides Enum trait to speed up the development of Laravel applications.
- Laravel 9.0 or later.
- PHP 8.1 or later.
Install via composer:
composer require everully/laravel-enum-on-steroidsAdd the EnumOnSteroids trait to your Enum class to add some useful methods.
use Everully\LaravelEnumOnSteroids\EnumOnSteroids;
enum StringEnum: string
{
use EnumOnSteroids;
case A = 'a';
case B = 'b';
case C = 'c';
}Check if the enum is equal to another enum or a string.
StringEnum::A->equals(StringEnum::A); // true
StringEnum::A->equals('a'); // true
StringEnum::A->equals(AnotherEnum::A); // false
StringEnum::A->equals('d'); // falseReturn an array all the values of the enum.
StringEnum::values(); // ['a', 'b', 'c']Return an array of all the names of the enum.
StringEnum::names(); // ['A', 'B', 'C']Return a Laravel collection of all the values of the enum.
StringEnum::collection();
// Illuminate\Support\Collection<StringEnum>Return a Laravel collection of all the values of the enum.
StringEnum::collect(['a', 'b', 'c']);
// Illuminate\Support\Collection<StringEnum>
StringEnum::collect([StringEnum::A, StringEnum::B, StringEnum::C]);
// Illuminate\Support\Collection<StringEnum>If provided string or object is not a valid enum value, the collection will not contain it.
StringEnum::collect(['a', 'invalid']);
// Only contains 'a'
StringEnum::collect([StringEnum::A, AnotherEnum::B]);
// Only contains 'a'Returns true if the enum has the provided value.
StringEnum::has('a'); // true
StringEnum::has(StringEnum::A); // true
StringEnum::has('invalid'); // false
StringEnum::has(AnotherEnum::A); // falseReturns true if the enum has any of the provided values.
StringEnum::hasAny(['a', 'invalid']); // true
StringEnum::hasAny([StringEnum::A, AnotherEnum::A]); // true
StringEnum::hasAny(['invalid', 'invalid2']); // false
StringEnum::hasAny([CopyStringEnum::A, CopyStringEnum::A]); // falseReturns true if the enum has all the provided values.
StringEnum::hasAny(['a', 'b']); // true
StringEnum::hasAny([StringEnum::A, AnotherEnum::A]); // true
StringEnum::hasAny(['a', 'invalid']); // false
StringEnum::hasAny([StringEnum::A, CopyStringEnum::A]); // false