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
Option | Description |
---|---|
--name | Full name of the user |
--email | Email address (must be unique) |
--username | Username (must be unique) |
--password | Plain-text password (validated using Laravel’s default rules) |
--roles | Comma-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
- 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:
Field | Validation Rules |
---|---|
Name | Required, string, max:255 |
Required, valid email format, unique in users table | |
Username | Required, string, max:255, unique in users table |
Password | Required, 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
Feature | Description |
---|---|
Command name | peak:create-user |
Interactive | Yes, falls back to prompts if options are not passed |
Role assignment | Supports multi-role assignment via option or prompt |
Validation | Built-in input validation for security and uniqueness |
Suitable for automation | Yes, can be used in shell scripts, CI pipelines, and seeders |