laranail/console
Laranail Console extends Laravel's Artisan CLI with convenient commands and helpers for common development and maintenance tasks, streamlining terminal workflows and making it easier to manage your application from the command line.
Installation
composer require laranail/console
Publish the config (if needed):
php artisan vendor:publish --provider="Laranail\Console\ConsoleServiceProvider"
First Use Case: Basic Output
Inject the Console facade into a command or service:
use Laranail\Console\Console;
public function handle()
{
Console::info('Hello, Laravel!');
Console::success('Task completed!');
}
Key Entry Points
Console::line(), Console::table(), Console::tree()Console::prompt(), Console::confirm()Console::form()->submit()Console::progressBar()Check the documentation for API reference.
Structured CLI Output
Use Console::table() for tabular data (e.g., database records):
$users = User::all();
Console::table(['ID', 'Name', 'Email'], $users->toArray());
Interactive Prompts
Build dynamic commands with Console::prompt():
$name = Console::prompt('Enter your name');
$confirm = Console::confirm('Proceed?', false);
Forms for Complex Input Validate user input via forms:
$result = Console::form()
->text('name', 'Name', ['required'])
->email('email', 'Email')
->submit();
if ($result->valid()) {
$data = $result->data();
}
Progress Tracking Visualize long-running tasks:
$bar = Console::progressBar(100);
for ($i = 0; $i < 100; $i++) {
$bar->advance();
}
Laranail\Console\ConsoleCommand for pre-configured output.Console across classes.config/console.php):
'colors' => [
'info' => '#3498db',
'error' => '#e74c3c',
],
lang/vendor/console.php.Progress Bar Stuck
$bar->finish().try-finally:
$bar = Console::progressBar(100);
try {
// Work...
} finally {
$bar->finish();
}
Prompt Input Truncation
Console::prompt() may cut off.Console::input() for multi-line input or adjust terminal width.Form Validation Errors
->error() in forms:
$form->text('age', 'Age')->rule('numeric|min:18')
->error('Age must be 18+');
Color Inconsistencies
config/console.php:
'fallback_colors' => true,
Console::disableOutput() to debug logic without clutter.Console::raw('{output}')->log() to inspect formatted strings.Console::table() vs Console::tabular()).Custom Output Styles
Extend Laranail\Console\Output\Style:
class CustomStyle extends Style {
public function custom($text) {
return $this->wrap($text, '<fg=yellow>...</>');
}
}
Add Prompt Types
Implement Laranail\Console\Prompt\PromptInterface for custom prompts (e.g., password with masking).
Override Defaults
Bind custom implementations in ConsoleServiceProvider:
$this->app->bind('console', function () {
return new CustomConsole();
});
Tree/Box Customization
Modify templates in resources/views/vendor/console/ (if using Blade-like syntax).
How can I help you explore Laravel packages today?