Create a Role
Here is the complete documentation for the peak:create-role
command, structured in the same format as the previous one to maintain consistency.
Overview
The peak:create-role
command is a CLI utility designed to create new roles within your Laravel application using the spatie/laravel-permission
package. It also optionally allows assigning one or more permissions to the newly created role.
This command simplifies backend user management and is especially helpful during development or in administrative shell operations.
Command Usage
Command Signature
php artisan peak:create-role
This command is interactive and will guide you through each required step.
Interactive Prompts
When running the command, you will be prompted for:
- Role Name – The unique name of the role (e.g.,
admin
,editor
). - Assign Permissions – Confirmation to optionally assign permissions.
- Permission Selection – If confirmed, allows multi-select from all available permissions in the database.
Input Validation
The role name is validated before creation. The rules applied are:
Field | Validation Rules |
---|---|
name | Required, string, max:255, unique in roles.name |
If validation fails, an error message is shown and the command exits with a non-zero status.
Role Creation
Once validated, the role is created using the 'web'
guard:
Role::create([
'name' => $name,
'guard_name' => 'web',
]);
The guard ensures that roles and permissions are scoped correctly in multi-auth systems.
Permission Assignment (Optional)
If confirmed, you’ll be presented with a multiple choice list of all existing permissions. You can select as many as needed.
The command then:
- Converts the selected permission names into Permission models.
- Assigns them using
syncPermissions
. - Outputs each assigned permission for confirmation.
If no permissions exist, a warning is shown.
Output Example
php artisan peak:create-role
Enter the name of the new role:
> moderator
Do you want to assign permissions to this role? (yes/no) [yes]:
> yes
Select permissions to assign to the role (multiple selection allowed, separated by comma):
> view users, edit posts
Role 'moderator' created successfully.
Permissions assigned to the role:
Permission: view users
Permission: edit posts
Permissions have been successfully assigned to the role.
Use Case Scenarios
1. Initial Role Setup
Run once per environment to seed roles like admin
, moderator
, editor
, etc.
2. Managing Permissions Easily
Avoid writing migrations or database seeders manually for role/permission management. Do it via CLI.
3. Admin Panel Replacement
Use this command when no admin dashboard is available to manage roles and permissions.
Summary
Feature | Description |
---|---|
Command name | peak:create-role |
Interactive | Yes |
Validation | Role name must be unique, required, and under 255 characters |
Guard | Uses web guard |
Permission assignment | Optional multi-select from available permissions |
Automation friendly | Yes, but interaction makes it better suited for human use |