Skip to content

Update user role logic to use enum field instead of role_id foreign key #143

Description

@DenTray

Description

What needs to be done?

Update the package so that when initializing a new project it:

  1. Generates a migration that adds an enum role field (values: 'admin', 'user') to the users table instead of creating a role_id field and a relationship with the roles table.
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->enum('role', ['admin', 'user'])->nullable();
});
}

public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('role');
});
}

2) Generates a RoleEnum enum class with values Admin = 'admin' and User= 'user' at the appropriate location App\Enums\User\RoleEnum.php

3) Generates the ClerkUserRepository class with role_id replaced by role

Before:

class ClerkUserRepository extends UserRepository
{
public function fromToken(Token $token): User
{
$user = parent::fromToken($token);

    return User::firstOrCreate([
        'clerk_id' => $user->getAuthIdentifier(),
    ], [
        'role_id' => Role::USER,
    ]);
}

}

After:

class ClerkUserRepository extends UserRepository
{
public function fromToken(Token $token): User
{
$user = parent::fromToken($token);

    return User::firstOrCreate([
        'clerk_id' => $user->getAuthIdentifier(),
    ], [
        'role' => RoleEnum::User,
    ]);
}

}

4) Update the add_default_user.php migration to use the new role enum field instead of role_id.

5) Implements logic (executed after generating the role field) that updates User.php using the helper package ronasit/larabuilder:

    • Adds 'role' to the $fillable array.
    • Adds $casts property with 'role' => RoleEnum::class mapping.
    • Imports RoleEnum at the top of User.php via use App\Enums\User\RoleEnum;.

Expected outcome

What is the expected result?

  • A migration that creates an enum column role (values: 'admin', 'user') on the users table without role_id or any dependency on the roles table.
  • A RoleEnum enum file at app/Enums/User/RoleEnum.php used consistently across generated backend code.
  • A ClerkUserRepository that assigns user roles using the enum value.
  • A User.php model automatically updated to:

    • Allow mass assignment of role.
    • Cast the role attribute to RoleEnum.
    • Import RoleEnum properly.
  • A add_default_user.php migration that uses the role enum field instead of role_id.

Verification scenarios

How can this be tested?

  • Run the init command and verify the generated migration creates the role enum column with correct values and no role_id.
  • Confirm the generated RoleEnum file exists and follows naming/location conventions.
  • Verify the generated ClerkUserRepository::fromToken method assigns the role via enum value correctly and creates or finds the user by clerk_id.
  • Check the generated User.php contains 'role' in $fillable, has the proper $casts entry, and imports the enum.
  • Run the generated project's migrations and verify the system functions normally with roles handled via the enum field.
  • Test migration rollback works without errors in the generated project.
  • Ensure no debug info or errors in application logs or console

Resources

Materials (Screenshots, Figma Links, References)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions