- Introduction
- Installation
- Available Prompts
- Informational Messages
- Tables
- Spin
- Progress Bar
- Terminal Considerations
- Unsupported Environments & Fallbacks
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.
Note
Laravel Prompts supports macOS, Linux, and Windows with WSL. For more information, please see our documentation on unsupported environments & fallbacks.
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
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.'
);
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.'
);
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.
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.'
);
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.'
);
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.
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.'
);
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.'
);
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(
'What role should the user have?',
['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
);
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.
If you need to the user to be able to select multiple options, you may use the multiselect
function:
use function Laravel\Prompts\multiselect;
$permissions = multiselect(
'What permissions should be assigned?',
['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
);
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',
);
You may pass a closure to the validate
argument if you need to present an option but prevent it from being selected:
$permissions = select(
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.
The suggest
function can be used to provide auto-completion for possible choices. The user can still provide any answer, regardless of the auto-completion hints:
use function Laravel\Prompts\suggest;
$name = suggest('What is your name?', ['Taylor', 'Dayle']);
Alternatively, you may pass a closure as the second argument to the suggest
function. The closure will be called each time the user types an input character. The closure should accept a string parameter containing the user's input so far and return an array of options for auto-completion:
$name = suggest(
'What is your name?',
fn ($value) => collect(['Taylor', 'Dayle'])
->filter(fn ($name) => Str::contains($name, $value, ignoreCase: true))
)
You may also include placeholder text, a default value, and an informational hint:
$name = suggest(
label: 'What is your name?',
options: ['Taylor', 'Dayle'],
placeholder: 'E.g. Taylor',
default: $user?->name,
hint: 'This will be displayed on your profile.'
);
If you require a value to be entered, you may pass the required
argument:
$name = suggest(
label: 'What is your name?',
options: ['Taylor', 'Dayle'],
required: true
);
If you would like to customize the validation message, you may also pass a string:
$name = suggest(
label: 'What is your name?',
options: ['Taylor', 'Dayle'],
required: 'Your name is required.'
);
Finally, if you would like to perform additional validation logic, you may pass a closure to the validate
argument:
$name = suggest(
label: 'What is your name?',
options: ['Taylor', 'Dayle'],
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.
If you have a lot of options for the user to select from, the search
function allows the user to type a search query to filter the results before using the arrow keys to select an option:
use function Laravel\Prompts\search;
$id = search(
'Search for the user that should receive the mail',
fn (string $value) => strlen($value) > 0
? User::where('name', 'like', "%{$value}%")->pluck('name', 'id')->all()
: []
);
The closure will receive the text that has been typed by the user so far and must return an array of options. If you return an associative array then the selected option's key will be returned, otherwise its value will be returned instead.
You may also include placeholder text and an informational hint:
$id = search(
label: 'Search for the user that should receive the mail',
placeholder: 'E.g. Taylor Otwell',
options: fn (string $value) => strlen($value) > 0
? User::where('name', 'like', "%{$value}%")->pluck('name', 'id')->all()
: [],
hint: 'The user will receive an email immediately.'
);
Up to five options will be displayed before the list begins to scroll. You may customize this by passing the scroll
argument:
$id = search(
label: 'Search for the user that should receive the mail',
options: fn (string $value) => strlen($value) > 0
? User::where('name', 'like', "%{$value}%")->pluck('name', 'id')->all()
: [],
scroll: 10
);
If you would like to perform additional validation logic, you may pass a closure to the validate
argument:
$id = search(
label: 'Search for the user that should receive the mail',
options: fn (string $value) => strlen($value) > 0
? User::where('name', 'like', "%{$value}%")->pluck('name', 'id')->all()
: [],
validate: function (int|string $value) {
$user = User::findOrFail($value);
if ($user->opted_out) {
return 'This user has opted-out of receiving mail.';
}
}
);
If the options
closure returns 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.
If you have a lot of searchable options and need the user to be able to select multiple items, the multisearch
function allows the user to type a search query to filter the results before using the arrow keys and space-bar to select options:
use function Laravel\Prompts\multisearch;
$ids = multisearch(
'Search for the users that should receive the mail',
fn (string $value) => strlen($value) > 0
? User::where('name', 'like', "%{$value}%")->pluck('name', 'id')->all()
: []
);
The closure will receive the text that has been typed by the user so far and must return an array of options. If you return an associative array then the selected options' keys will be returned; otherwise, their values will be returned instead.
You may also include placeholder text and an informational hint:
$ids = multisearch(
label: 'Search for the users that should receive the mail',
placeholder: 'E.g. Taylor Otwell',
options: fn (string $value) => strlen($value) > 0
? User::where('name', 'like', "%{$value}%")->pluck('name', 'id')->all()
: [],
hint: 'The user will receive an email immediately.'
);
Up to five options will be displayed before the list begins to scroll. You may customize this by providing the scroll
argument:
$ids = multisearch(
label: 'Search for the users that should receive the mail',
options: fn (string $value) => strlen($value) > 0
? User::where('name', 'like', "%{$value}%")->pluck('name', 'id')->all()
: [],
scroll: 10
);
By default, the user may select zero or more options. You may pass the required
argument to enforce one or more options instead:
$ids = multisearch(
'Search for the users that should receive the mail',
fn (string $value) => strlen($value) > 0
? User::where('name', 'like', "%{$value}%")->pluck('name', 'id')->all()
: [],
required: true,
);
If you would like to customize the validation message, you may also provide a string to the required
argument:
$ids = multisearch(
'Search for the users that should receive the mail',
fn (string $value) => strlen($value) > 0
? User::where('name', 'like', "%{$value}%")->pluck('name', 'id')->all()
: [],
required: 'You must select at least one user.'
);
If you would like to perform additional validation logic, you may pass a closure to the validate
argument:
$ids = multisearch(
label: 'Search for the users that should receive the mail',
options: fn (string $value) => strlen($value) > 0
? User::where('name', 'like', "%{$value}%")->pluck('name', 'id')->all()
: [],
validate: function (array $values) {
$optedOut = User::where('name', 'like', '%a%')->findMany($values);
if ($optedOut->isNotEmpty()) {
return $optedOut->pluck('name')->join(', ', ', and ').' have opted out.';
}
}
);
If the options
closure returns 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.
The note
, info
, warning
, error
, and alert
functions may be used to display informational messages:
use function Laravel\Prompts\info;
info('Package installed successfully.');
The table
function makes it easy to display multiple rows and columns of data. All you need to do is provide the column names and the data for the table:
use function Laravel\Prompts\table;
table(
['Name', 'Email'],
User::all(['name', 'email'])
);
The spin
function displays a spinner along with an optional message while executing a specified callback. It serves to indicate ongoing processes and returns the callback's results upon completion:
use function Laravel\Prompts\spin;
$response = spin(
fn () => Http::get('http://example.com'),
'Fetching response...'
);
Warning
Thespin
function requires thepcntl
PHP extension to animate the spinner. When this extension is not available, a static version of the spinner will appear instead.
For long running tasks, it can be helpful to show a progress bar that informs users how complete the task is. Using the progress
function, Laravel will display a progress bar and advance its progress for each iteration over a given iterable value:
use function Laravel\Prompts\progress;
$users = progress(
label: 'Updating users',
steps: User::all(),
callback: fn ($user) => $this->performTask($user),
);
The progress
function acts like a map function and will return an array containing the return value of each iteration of your callback.
The callback may also accept the \Laravel\Prompts\Progress
instance, allowing you to modify the label and hint on each iteration:
$users = progress(
label: 'Updating users',
steps: User::all(),
callback: function ($user, $progress) {
$progress
->label("Updating {$user->name}")
->hint("Created on {$user->created_at}");
return $this->performTask($user);
},
hint: 'This may take some time.',
);
Sometimes, you may need more manual control over how a progress bar is advanced. First, define the total number of steps the process will iterate through. Then, advance the progress bar via the advance
method after processing each item:
$progress = progress(label: 'Updating users', steps: 10);
$users = User::all();
$progress->start();
foreach ($users as $user) {
$this->performTask($user);
$progress->advance();
}
$progress->finish();
If the length of any label, option, or validation message exceeds the number of "columns" in the user's terminal, it will be automatically truncated to fit. Consider minimizing the length of these strings if your users may be using narrower terminals. A typically safe maximum length is 74 characters to support an 80-character terminal.
For any prompts that accept the scroll
argument, the configured value will automatically be reduced to fit the height of the user's terminal, including space for a validation message.
Laravel Prompts supports macOS, Linux, and Windows with WSL. Due to limitations in the Windows version of PHP, it is not currently possible to use Laravel Prompts on Windows outside of WSL.
For this reason, Laravel Prompts supports falling back to an alternative implementation such as the Symfony Console Question Helper.
Note
When using Laravel Prompts with the Laravel framework, fallbacks for each prompt have been configured for you and will be automatically enabled in unsupported environments.
If you are not using Laravel or need to customize when the fallback behavior is used, you may pass a boolean to the fallbackWhen
static method on the Prompt
class:
use Laravel\Prompts\Prompt;
Prompt::fallbackWhen(
! $input->isInteractive() || windows_os() || app()->runningUnitTests()
);
If you are not using Laravel or need to customize the fallback behavior, you may pass a closure to the fallbackUsing
static method on each prompt class:
use Laravel\Prompts\TextPrompt;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
TextPrompt::fallbackUsing(function (TextPrompt $prompt) use ($input, $output) {
$question = (new Question($prompt->label, $prompt->default ?: null))
->setValidator(function ($answer) use ($prompt) {
if ($prompt->required && $answer === null) {
throw new \RuntimeException(is_string($prompt->required) ? $prompt->required : 'Required.');
}
if ($prompt->validate) {
$error = ($prompt->validate)($answer ?? '');
if ($error) {
throw new \RuntimeException($error);
}
}
return $answer;
});
return (new SymfonyStyle($input, $output))
->askQuestion($question);
});
Fallbacks must be configured individually for each prompt class. The closure will receive an instance of the prompt class and must return an appropriate type for the prompt.