Prompts
Introduction
Laravel Prompts is a PHP package for adding beautiful and user-friendly forms to your command-line applications, with browser-like features including placeholder text and validation.
Laravel Prompts is perfect for accepting user input in your Artisan console commands, but it may also be used in any command-line PHP project.
Laravel Prompts supports macOS, Linux, and Windows with WSL. For more information, please see our documentation on unsupported environments & fallbacks.
Installation
Laravel Prompts is already included with the latest release of Laravel.
Laravel Prompts may also be installed in your other PHP projects by using the Composer package manager:
composer require laravel/prompts
Available Prompts
Text
The text
function will prompt the user with the given question, accept their input, and then return it:
use function Laravel\Prompts\text;
$name = text('What is your name?');
You may also include placeholder text, a default value, and an informational hint:
$name = text(
label: 'What is your name?',
placeholder: 'E.g. Taylor Otwell',
default: $user?->name,
hint: 'This will be displayed on your profile.'
);
Required Values
If you require a value to be entered, you may pass the required
argument:
$name = text(
label: 'What is your name?',
required: true
);
If you would like to customize the validation message, you may also pass a string:
$name = text(
label: 'What is your name?',
required: 'Your name is required.'
);
Additional Validation
Finally, if you would like to perform additional validation logic, you may pass a closure to the validate
argument:
$name = text(
label: 'What is your name?',
validate: fn (string $value) => match (true) {
strlen($value) < 3 => 'The name must be at least 3 characters.',
strlen($value) > 255 => 'The name must not exceed 255 characters.',
default => null
}
);
The closure will receive the value that has been entered and may return an error message, or null
if the validation passes.
Alternatively, you may leverage the power of Laravel's validator. To do so, provide an array containing the name of the attribute and the desired validation rules to the validate
argument:
$name = text(
label: 'What is your name?',
validate: ['name' => 'required|max:255|unique:users']
);
Textarea
The textarea
function will prompt the user with the given question, accept their input via a multi-line textarea, and then return it:
use function Laravel\Prompts\textarea;
$story = textarea('Tell me a story.');
You may also include placeholder text, a default value, and an informational hint:
$story = textarea(
label: 'Tell me a story.',
placeholder: 'This is a story about...',
hint: 'This will be displayed on your profile.'
);
Required Values
If you require a value to be entered, you may pass the required
argument:
$story = textarea(
label: 'Tell me a story.',
required: true
);
If you would like to customize the validation message, you may also pass a string:
$story = textarea(
label: 'Tell me a story.',
required: 'A story is required.'
);
Additional Validation
Finally, if you would like to perform additional validation logic, you may pass a closure to the validate
argument:
$story = textarea(
label: 'Tell me a story.',
validate: fn (string $value) => match (true) {
strlen($value) < 250 => 'The story must be at least 250 characters.',
strlen($value) > 10000 => 'The story must not exceed 10,000 characters.',
default => null
}
);
The closure will receive the value that has been entered and may return an error message, or null
if the validation passes.
Alternatively, you may leverage the power of Laravel's validator. To do so, provide an array containing the name of the attribute and the desired validation rules to the validate
argument:
$story = textarea(
label: 'Tell me a story.',
validate: ['story' => 'required|max:10000']
);
Password
The password
function is similar to the text
function, but the user's input will be masked as they type in the console. This is useful when asking for sensitive information such as passwords:
use function Laravel\Prompts\password;
$password = password('What is your password?');
You may also include placeholder text and an informational hint:
$password = password(
label: 'What is your password?',
placeholder: 'password',
hint: 'Minimum 8 characters.'
);
Required Values
If you require a value to be entered, you may pass the required
argument:
$password = password(
label: 'What is your password?',
required: true
);
If you would like to customize the validation message, you may also pass a string:
$password = password(
label: 'What is your password?',
required: 'The password is required.'
);
Additional Validation
Finally, if you would like to perform additional validation logic, you may pass a closure to the validate
argument:
$password = password(
label: 'What is your password?',
validate: fn (string $value) => match (true) {
strlen($value) < 8 => 'The password must be at least 8 characters.',
default => null
}
);
The closure will receive the value that has been entered and may return an error message, or null
if the validation passes.
Alternatively, you may leverage the power of Laravel's validator. To do so, provide an array containing the name of the attribute and the desired validation rules to the validate
argument:
$password = password(
label: 'What is your password?',
validate: ['password' => 'min:8']
);
Confirm
If you need to ask the user for a "yes or no" confirmation, you may use the confirm
function. Users may use the arrow keys or press y
or n
to select their response. This function will return either true
or false
.
use function Laravel\Prompts\confirm;
$confirmed = confirm('Do you accept the terms?');
You may also include a default value, customized wording for the "Yes" and "No" labels, and an informational hint:
$confirmed = confirm(
label: 'Do you accept the terms?',
default: false,
yes: 'I accept',
no: 'I decline',
hint: 'The terms must be accepted to continue.'
);
Requiring "Yes"
If necessary, you may require your users to select "Yes" by passing the required
argument:
$confirmed = confirm(
label: 'Do you accept the terms?',
required: true
);
If you would like to customize the validation message, you may also pass a string:
$confirmed = confirm(
label: 'Do you accept the terms?',
required: 'You must accept the terms to continue.'
);
Select
If you need the user to select from a predefined set of choices, you may use the select
function:
use function Laravel\Prompts\select;
$role = select(
label: 'What role should the user have?',
options: ['Member', 'Contributor', 'Owner']
);
You may also specify the default choice and an informational hint:
$role = select(
label: 'What role should the user have?',
options: ['Member', 'Contributor', 'Owner'],
default: 'Owner',
hint: 'The role may be changed at any time.'
);
You may also pass an associative array to the options
argument to have the selected key returned instead of its value:
$role = select(
label: 'What role should the user have?',
options: [
'member' => 'Member',
'contributor' => 'Contributor',
'owner' => 'Owner',
],
default: 'owner'
);
Up to five options will be displayed before the list begins to scroll. You may customize this by passing the scroll
argument:
$role = select(
label: 'Which category would you like to assign?',
options: Category::pluck('name', 'id'),
scroll: 10
);
Additional Validation
Unlike other prompt functions, the select
function doesn't accept the required
argument because it is not possible to select nothing. However, you may pass a closure to the validate
argument if you need to present an option but prevent it from being selected:
$role = select(
label: 'What role should the user have?',
options: [
'member' => 'Member',
'contributor' => 'Contributor',
'owner' => 'Owner',
],
validate: fn (string $value) =>
$value === 'owner' && User::where('role', 'owner')->exists()
? 'An owner already exists.'
: null
);
If the options
argument is an associative array, then the closure will receive the selected key, otherwise it will receive the selected value. The closure may return an error message, or null
if the validation passes.
Multi-select
If you need the user to be able to select multiple options, you may use the multiselect
function:
use function Laravel\Prompts\multiselect;
$permissions = multiselect(
label: 'What permissions should be assigned?',
options: ['Read', 'Create', 'Update', 'Delete']
);
You may also specify default choices and an informational hint:
use function Laravel\Prompts\multiselect;
$permissions = multiselect(
label: 'What permissions should be assigned?',
options: ['Read', 'Create', 'Update', 'Delete'],
default: ['Read', 'Create'],
hint: 'Permissions may be updated at any time.'
);
You may also pass an associative array to the options
argument to return the selected options' keys instead of their values:
$permissions = multiselect(
label: 'What permissions should be assigned?',
options: [
'read' => 'Read',
'create' => 'Create',
'update' => 'Update',
'delete' => 'Delete',
],
default: ['read', 'create']
);
Up to five options will be displayed before the list begins to scroll. You may customize this by passing the scroll
argument:
$categories = multiselect(
label: 'What categories should be assigned?',
options: Category::pluck('name', 'id'),
scroll: 10
);
Requiring a Value
By default, the user may select zero or more options. You may pass the required
argument to enforce one or more options instead:
$categories = multiselect(
label: 'What categories should be assigned?',
options: Category::pluck('name', 'id'),
required: true
);
If you would like to customize the validation message, you may provide a string to the required
argument:
$categories = multiselect(
label: 'What categories should be assigned?',
options: Category::pluck('name', 'id'),
required: 'You must select at least one category'
);
Additional Validation
You may pass a closure to the validate
argument if you need to present an option but prevent it from being selected:
$permissions = multiselect(
label: 'What permissions should the user have?',
options: [
'read' => 'Read',
'create' => 'Create',
'update' => 'Update',
'delete' => 'Delete',
],
validate: fn (array $values) => ! in_array('read', $values)
? 'All users require the read permission.'
: null
);
If the options
argument is an associative array then the closure will receive the selected keys, otherwise it will receive the selected values. The closure may return an error message, or null
if the validation passes.