use function Laravel\Prompts\text;$name = text('What is your name?');
可以设置占位符、默认值和提示。
$name = text( label: 'What is your name?', placeholder: 'E.g. Taylor Otwell', default: $user?->name, hint: 'This will be displayed on your profile.');
指定 required 可将输入设为必填,并自定义错误提示。
$name = text( label: 'What is your name?', required: 'Your name is required.');
validate 闭包用来做额外校验。返回错误消息即校验失败,返回 null 表示通过。
$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 });
也可以以数组形式使用 Laravel 校验规则。
$name = text( label: 'What is your name?', validate: ['name' => 'required|max:255|unique:users']);
use function Laravel\Prompts\number;$copies = number( label: 'How many copies would you like?', default: 1, validate: ['copies' => 'required|integer|min:1|max:100']);
use function Laravel\Prompts\password;$password = password('What is your password?');
$password = password( label: 'What is your password?', placeholder: 'password', hint: 'Minimum 8 characters.', validate: fn (string $value) => strlen($value) < 8 ? 'The password must be at least 8 characters.' : null);
use function Laravel\Prompts\confirm;$confirmed = confirm('Do you accept the terms?');
可以自定义默认值和标签文案。
$confirmed = confirm( label: 'Do you accept the terms?', default: false, yes: 'I accept', no: 'I decline', hint: 'The terms must be accepted to continue.');
use function Laravel\Prompts\select;$role = select( label: 'What role should the user have?', options: ['Member', 'Contributor', 'Owner'], default: 'Owner');
如果传入关联数组,返回值就是键而不是展示的标签。
$role = select( label: 'What role should the user have?', options: [ 'member' => 'Member', 'contributor' => 'Contributor', 'owner' => 'Owner', ], default: 'owner');
scroll 用来指定滚动前显示的选项数量(默认 5)。
$role = select( label: 'Which category would you like to assign?', options: Category::pluck('name', 'id'), scroll: 10);
use function Laravel\Prompts\multiselect;$permissions = multiselect( label: 'What permissions should be assigned?', options: ['Read', 'Create', 'Update', 'Delete']);
指定 required 可要求至少选择一个。
$permissions = multiselect( label: 'What permissions should be assigned?', options: ['Read', 'Create', 'Update', 'Delete'], required: 'At least one permission must be selected.');
use function Laravel\Prompts\search;$userId = search( label: 'Search for the user that should receive the mail', options: fn (string $value) => strlen($value) > 0 ? User::whereLike('name', "%{$value}%")->pluck('name', 'id')->all() : []);
use function Laravel\Prompts\multisearch;$userIds = multisearch( label: 'Search for the users that should receive the mail', options: fn (string $value) => strlen($value) > 0 ? User::whereLike('name', "%{$value}%")->pluck('name', 'id')->all() : []);
$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 });
$name = text( label: 'What is your name?', validate: ['name' => 'required|max:255|unique:users']);
若要在校验前先转换输入,使用 transform 参数。
$name = text( label: 'What is your name?', transform: fn (string $value) => trim($value), validate: fn (string $value) => match (true) { strlen($value) === 0 => 'The name must not be empty.', default => null });
use function Laravel\Prompts\form;$responses = form() ->text('What is your name?', required: true, name: 'name') ->password('What is your password?', validate: ['password' => 'min:8'], name: 'password') ->confirm('Do you accept the terms?') ->submit();$name = $responses['name'];$password = $responses['password'];$confirmed = $responses[2];
use function Laravel\Prompts\info;use function Laravel\Prompts\warning;use function Laravel\Prompts\error;use function Laravel\Prompts\alert;use function Laravel\Prompts\note;note('Prepare for launch.');info('User created successfully.');warning('This action cannot be undone.');error('Something went wrong.');alert('Critical failure detected!');
use function Laravel\Prompts\callout;callout( label: 'Environment Configured', content: 'Your application is running in production mode with 4 workers.',);
在 type 参数中传入 'warning' 或 'error' 可以改变视觉样式。
callout( label: 'Deprecation Notice', content: 'The `--prefer-stable` flag will be removed in v4.0.', type: 'warning',);callout( label: 'Database Connection Failed', content: 'Could not connect to MySQL on 127.0.0.1:3306.', type: 'error',);
通过 info 参数可以添加尾行。方便展示 ID、时间戳等元数据。
callout( label: 'Deployment Summary', content: 'Your application was deployed to production.', info: 'deploy-id: d4f8a2c',);
use Laravel\Prompts\Elements\Element;use function Laravel\Prompts\callout;callout('Deployment Summary', [ 'Your application was deployed to production at 2024-03-15 14:32 UTC.', Element::heading('What Changed'), Element::bulletedList([ 'Migrated 3 pending database migrations', 'Cleared and rebuilt route cache', 'Restarted 4 queue workers', ]), Element::heading('Next Steps'), Element::numberedList([ 'Verify the health check endpoint at /up', 'Monitor error rates for the next 15 minutes', 'Confirm background jobs are processing', ]),]);
Element::keyValueList 可以展示带标签的数据。
callout('Database Connection Failed', [ 'Could not connect to the database server.', Element::keyValueList([ 'Host' => '127.0.0.1', 'Port' => '3306', 'Database' => 'forge', 'Status' => 'Connection refused', ]),], type: 'error');
use function Laravel\Prompts\progress;$users = progress( label: 'Updating users', steps: User::all(), callback: fn ($user) => $this->performTask($user), hint: 'This may take some time.');
use Laravel\Prompts\Prompt;Prompt::fake(['Taylor', true]);$name = text('What is your name?');$confirmed = confirm('Do you accept the terms?');Prompt::assertOutputContains('What is your name?');
使用 Laravel 的 Artisan 测试辅助方法,也可以对信息输出函数进行断言。
// Pesttest('report generation', function () { $this->artisan('report:generate') ->expectsPromptsInfo('Welcome to the application!') ->expectsPromptsWarning('This action cannot be undone') ->expectsPromptsError('Something went wrong') ->expectsPromptsAlert('Important notice!') ->expectsPromptsTable( headers: ['Name', 'Email'], rows: [ ['Taylor Otwell', '[email protected]'], ] ) ->assertExitCode(0);});
// PHPUnitpublic function test_report_generation(): void{ $this->artisan('report:generate') ->expectsPromptsInfo('Welcome to the application!') ->expectsPromptsWarning('This action cannot be undone') ->expectsPromptsTable( headers: ['Name', 'Email'], rows: [['Taylor Otwell', '[email protected]']] ) ->assertExitCode(0);}