Open Source 33 / 34
Create a User
Learn how you can host your website and make it live for the world to see.
Create a User
The app:create-user command allows you to create a new user with role assignment through an interactive command-line interface. This command is part of the Prompts section and utilizes Laravel's built-in prompt methods to gather user information.
Command Signature
php artisan app:create-user
Usage
When you run the command, it will prompt you for the following information:
- User's name
- User's email
- User's username
- User's password (input will be hidden)
After providing this information, the command will validate the input. If validation passes, it will create the user and then prompt you to select a role for the new user.
Step-by-step Process
- Run the command in your terminal:
php artisan app:create-user
-
Enter the requested information when prompted:
- Name
- Username
- Password (input will be hidden)
-
If any validation errors occur, the command will display them and exit. You'll need to run the command again with valid input.
-
After successful user creation, you'll be presented with a list of available roles. Select a role for the new user.
-
The command will assign the selected role to the user and display a success message.
Validation Rules
The command applies the following validation rules:
- Name: Required, string, maximum 255 characters
- Email: Required, valid email format, maximum 255 characters, must be unique in the users table
- Username: Required, string, maximum 255 characters, must be unique in the users table
- Password: Required, must meet the default password requirements set in your Laravel application
Role Assignment
The command fetches all available roles from the database and allows you to choose one for the new user. The user is assigned only the selected role, removing any previously assigned roles.
Success Output
Upon successful user creation and role assignment, the command will output a message like this:
User created successfully with role: [Selected Role Name]
Error Handling
If validation fails, the command will display all validation errors and exit with a status code of 1. You'll need to run the command again with correct input.
Notes
- The user is automatically marked as verified (
verifiedcolumn set to 1). - This command uses the Spatie Permission package for role management. Ensure that this package is properly installed and configured in your Laravel application.
- The command syncs roles, meaning it will remove any existing roles before assigning the new one.
Remember to use this command responsibly, especially in production environments, as it creates fully verified users with assigned roles.