Skip to main content

Create a User

Overview

The peak:create-user command provides a convenient way to create a new user account directly from the command line. It supports both interactive prompts and inline options, and allows assigning one or multiple roles during creation.

This command is particularly useful for:

  • Seeding admin users during development
  • Creating accounts in production via CLI
  • Automating role-based account setups through scripts

Command Usage

Command Signature

php artisan peak:create-user [--name=] [--email=] [--username=] [--password=] [--roles=]

Options

OptionDescription
--nameFull name of the user
--emailEmail address (must be unique)
--usernameUsername (must be unique)
--passwordPlain-text password (validated using Laravel’s default rules)
--rolesComma-separated list of roles to assign to the user

If any of the options are omitted, the command will prompt interactively for them.


Interactive Prompts

When the required options are not passed, the command will ask:

  • Name
  • Email
  • Username
  • Password (input hidden for security)
  • Roles (multi-select from available roles)

Input Validation

The command uses Laravel’s Validator to enforce the following rules:

FieldValidation Rules
NameRequired, string, max:255
EmailRequired, valid email format, unique in users table
UsernameRequired, string, max:255, unique in users table
PasswordRequired, must meet default password strength policy

If validation fails, all error messages are displayed and the command exits with a non-zero code.


Role Management

After user creation:

  • All available roles are retrieved from the database.

  • If --roles is passed:

    • It is split by comma and trimmed.
    • Any roles not found in the database are rejected with an error.
  • If no --roles is passed:

    • A multi-select prompt allows choosing roles interactively.
  • The user is assigned the selected roles using assignRole.


Output Example

php artisan peak:create-user --name="Alice Doe" --email="[email protected]" --username="alice" --password="secret123" --roles=admin,moderator

If successful, you will see:

User created successfully with roles: admin, moderator

If a role does not exist:

Invalid roles provided: superadmin

Use Case Scenarios

1. Seeding a Super Admin

You can seed an admin using Laravel's seeder system and call this command using Artisan::call() inside a seeder or setup script.

2. Docker or CI/CD Setup

Useful for prepopulating admin users or developers when bootstrapping a Laravel app.

php artisan peak:create-user --name="Dev Admin" --email="[email protected]" --username="admin" --password="password" --roles=admin

Summary

FeatureDescription
Command namepeak:create-user
InteractiveYes, falls back to prompts if options are not passed
Role assignmentSupports multi-role assignment via option or prompt
ValidationBuilt-in input validation for security and uniqueness
Suitable for automationYes, can be used in shell scripts, CI pipelines, and seeders