Skip to content

Commit 1ad80bb

Browse files
authored
Enum trait (#116)
* Create Enum.php * Use enum * Move enum to concern sub directory * Use specific enum values in migrations Fixes issues where you add new enum values later but migrations have already been run
1 parent 046517a commit 1ad80bb

File tree

8 files changed

+54
-32
lines changed

8 files changed

+54
-32
lines changed

app/Enums/Concerns/Enum.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Enums\Concerns;
4+
5+
use Illuminate\Support\Collection;
6+
7+
trait Enum
8+
{
9+
public static function values(): Collection
10+
{
11+
return collect(self::cases())->map(fn ($case): string => $case->value);
12+
}
13+
14+
public static function only(array $cases, bool $asArray = false): Collection
15+
{
16+
return collect(self::cases())->filter(fn ($case) => in_array($case, $cases))->map(fn ($case) => $asArray ? $case->value : $case);
17+
}
18+
}

app/Enums/Environment.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22

33
namespace App\Enums;
44

5-
use Illuminate\Support\Collection;
5+
use App\Enums\Concerns\Enum;
66

77
enum Environment: string
88
{
9+
use Enum;
10+
911
case LOCAL = 'local';
1012
case TESTING = 'testing';
1113
case PRODUCTION = 'production';
1214
case STAGING = 'staging';
13-
14-
public static function values(): Collection
15-
{
16-
return collect(self::cases())->map(fn ($case): string => $case->value);
17-
}
1815
}

app/Enums/Permission.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
namespace App\Enums;
44

5+
use App\Enums\Concerns\Enum;
6+
57
enum Permission: string
68
{
9+
use Enum;
10+
711
case ACCESS_ADMIN = 'access-filament';
812

913
case CREATE_POSTS = 'create-posts';
1014
case VIEW_POSTS = 'view-posts';
1115
case EDIT_POSTS = 'edit-posts';
1216
case UPDATE_POSTS = 'update-posts';
1317
case DELETE_POSTS = 'delete-posts';
14-
15-
public static function values(): array
16-
{
17-
return collect(self::cases())->map(fn ($case): string => $case->value)->toArray();
18-
}
1918
}

app/Enums/Queue.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
namespace App\Enums;
44

5-
use Illuminate\Support\Collection;
5+
use App\Enums\Concerns\Enum;
66

77
enum Queue: string
88
{
9+
use Enum;
10+
911
case DEFAULT = 'default';
1012
case MAIL = 'mail';
11-
12-
public static function values(): Collection
13-
{
14-
return collect(self::cases())->map(fn ($case): string => $case->value);
15-
}
1613
}

app/Enums/Role.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
namespace App\Enums;
44

5+
use App\Enums\Concerns\Enum;
6+
57
enum Role: string
68
{
9+
use Enum;
10+
711
case SUPER_ADMIN = 'super-admin';
812
case ADMIN = 'admin';
913
case USER = 'user';
10-
11-
public static function values(): array
12-
{
13-
return collect(self::cases())->map(fn ($case): string => $case->value)->toArray();
14-
}
1514
}

app/Filament/Resources/Users/Tables/UsersTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static function configure(Table $table): Table
6262
SelectFilter::make('roles')
6363
->label('Roles')
6464
->relationship('roles', 'name')
65-
->options(Role::values())
65+
->options(Role::values()->all())
6666
->multiple()
6767
->preload(),
6868
])

database/factories/UserFactory.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Enums\Role;
66
use App\Models\User;
77
use Illuminate\Database\Eloquent\Factories\Factory;
8+
use Illuminate\Support\Facades\Hash;
89
use Illuminate\Support\Str;
910

1011
/**
@@ -19,7 +20,7 @@ public function definition()
1920
'last_name' => \fake()->lastName(),
2021
'email' => \fake()->unique()->safeEmail(),
2122
'email_verified_at' => \now(),
22-
'password' => '12345',
23+
'password' => Hash::make(config('seed.users.super.password')),
2324
'remember_token' => Str::random(10),
2425
];
2526
}
@@ -35,8 +36,8 @@ public function superAdmin(?string $email = null)
3536
{
3637
return $this
3738
->state(fn (array $attributes) => [
38-
'email' => \config('seed.users.super.email'),
39-
'password' => \config('seed.users.super.password'),
39+
'email' => config('seed.users.super.email'),
40+
'password' => Hash::make(config('seed.users.super.password')),
4041
])
4142
->afterCreating(function (User $user) {
4243
$user->assignRole(Role::SUPER_ADMIN);
@@ -47,8 +48,8 @@ public function admin()
4748
{
4849
return $this
4950
->state(fn (array $attributes) => [
50-
'email' => \config('seed.users.admin.email'),
51-
'password' => \config('seed.users.admin.password'),
51+
'email' => config('seed.users.admin.email'),
52+
'password' => Hash::make(config('seed.users.admin.password')),
5253
])
5354
->afterCreating(function (User $user) {
5455
$user->assignRole(Role::ADMIN);
@@ -59,8 +60,8 @@ public function user()
5960
{
6061
return $this
6162
->state(fn (array $attributes) => [
62-
'email' => \config('seed.users.user.email'),
63-
'password' => \config('seed.users.user.password'),
63+
'email' => config('seed.users.user.email'),
64+
'password' => Hash::make(config('seed.users.user.password')),
6465
])
6566
->afterCreating(function (User $user) {
6667
$user->assignRole(Role::USER);

database/migrations/0001_01_01_000007_create_permission_tables.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ public function up(): void
2424

2525
Schema::create($tableNames['permissions'], function (Blueprint $table) {
2626
$table->bigIncrements('id'); // permission id
27-
$table->enum('name', Permission::values());
27+
$table->enum('name', Permission::only([
28+
Permission::ACCESS_ADMIN,
29+
Permission::CREATE_POSTS,
30+
Permission::VIEW_POSTS,
31+
Permission::EDIT_POSTS,
32+
Permission::UPDATE_POSTS,
33+
Permission::DELETE_POSTS,
34+
], true)->all());
2835
$table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);
2936
$table->timestamps();
3037

@@ -37,7 +44,11 @@ public function up(): void
3744
$table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable();
3845
$table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index');
3946
}
40-
$table->enum('name', Role::values());
47+
$table->enum('name', Role::only([
48+
Role::SUPER_ADMIN,
49+
Role::ADMIN,
50+
Role::USER,
51+
], true)->all());
4152
$table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);
4253
$table->timestamps();
4354
if ($teams || config('permission.testing')) {

0 commit comments

Comments
 (0)