Installation:
composer require eddiriarte/console-select
Laravel/Laravel-Zero:
Register the service provider in config/app.php:
'providers' => [
EddIriarte\Console\Providers\SelectServiceProvider::class,
],
Symfony: Add the trait and helper to your command:
use EddIriarte\Console\Traits\SelectableInputs;
use EddIriarte\Console\Helpers\SelectionHelper;
class MyCommand extends Command
{
use SelectableInputs;
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getHelperSet()->set(
new SelectionHelper($input, $output)
);
}
}
Use select() in a command to prompt the user for input:
$selected = $this->select(
'Choose a user role:',
['Admin', 'Editor', 'Viewer']
);
This will display an interactive selection menu with keyboard navigation.
Single Selection (Radio Mode):
Force single selection by passing false as the third argument:
$role = $this->select('Pick a role:', ['Admin', 'Editor', 'Viewer'], false);
Multi-Selection (Checkbox Mode): Default behavior allows multiple selections:
$roles = $this->select('Pick all applicable roles:', ['Admin', 'Editor', 'Viewer']);
Dynamic Options: Fetch options from a database or API before rendering:
$users = User::all()->pluck('name')->toArray();
$selected = $this->select('Select users:', $users);
Conditional Logic: Use selected values to drive workflows:
if (in_array('Admin', $selected)) {
$this->info('Granting admin privileges...');
}
Integration with Laravel Artisan: Create custom Artisan commands for admin tasks:
// app/Console/Commands/AssignRoles.php
public function handle()
{
$roles = $this->select('Assign roles:', ['Admin', 'Editor'], false);
// Logic to assign roles...
}
if (empty($selected)) {
$this->error('No selections made.');
return;
}
try {
$selected = $this->select('...');
} catch (\Exception $e) {
$this->error('Selection failed: ' . $e->getMessage());
}
No Multi-Line Support:
Options with newlines (\n) may render poorly. Use short labels or truncate long text.
Keyboard Navigation: Users expect arrow keys for navigation. Test with non-mouse users.
Empty Selections: The package doesn’t enforce non-empty selections. Validate manually:
if (empty($selected)) {
throw new \RuntimeException('No option selected.');
}
Symfony Integration:
Forgetting to add SelectionHelper to the helper set will throw errors. Always include:
$this->getHelperSet()->set(new SelectionHelper($this->input, $this->output));
Laravel Zero:
Ensure the service provider is registered before using $this->select().
$this->output->writeln() to debug option lists:
$this->output->writeln(print_r($options, true));
$this->input object if selections behave unexpectedly.php artisan [command] to test interactively.Custom Styling:
Override the default renderer by extending EddIriarte\Console\Renderers\SelectionRenderer:
class CustomRenderer extends SelectionRenderer
{
protected function renderOption($option, $selected)
{
// Custom logic here
}
}
Register it via the service provider.
Add Icons/Emojis: Prepend icons to options for visual cues:
$options = [
'👤 Admin',
'✏️ Editor',
'👁️ Viewer',
];
Localization: Translate prompts/options using Laravel’s translation system:
$prompt = __('Select a role');
$options = __('Select roles') => ['Admin', 'Editor'];
Async Data Loading: For large datasets, fetch options lazily (e.g., via AJAX in a custom renderer).
No Built-in Config: The package relies on Symfony’s console component and doesn’t expose config options. Customize via:
$this->select() in helper methods.Legacy Support: Test on Laravel 5.6+ and Symfony 5.0+. Older versions may require polyfills.
How can I help you explore Laravel packages today?