$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[March 29, 2025] Laravel

Laravel Console Output: Formatting Tips

Laravel Console Output: Formatting Tips

────────────────────────────────────────────────────────

Laravel Console Output: Formatting Tips

Tip: Progress Bar

$bar = $this->output->createProgressBar(count($users));
foreach ($users as $user) {
    process($user);
    $bar->advance();
}
$bar->finish();

Gotcha: Output Buffering

Large output can slow down terminals. Use $this->output->write() instead of echo.

Tip: Alert Boxes

$this->alert('WARNING', 'This operation cannot be undone!');

Gotcha: New Lines

$this->newLine(2);

Tip: Two-Column Display

$this->twoColumnDetail('Processing', '100 items');
$this->twoColumnDetail('Completed', 'Done');

Gotcha: Verbose Output

$this->info('Processing...', 'v');

Only shows when -v flag is passed.

Tip: Use route:cache Carefully

php artisan route:cache is fast, but it doesn't work with closure-based routes. Every time you cache routes, Laravel serializes them. If you have Route::redirect() or closure callbacks, the cache breaks. Stick to controller-based routes in production.

Tip: Model APP_KEY Rotation

Rotating APP_KEY invalidates all encrypted data — cookies, encrypted DB columns, and password reset tokens. If you must rotate (e.g., after a leak), plan a migration that re-encrypts existing data with the new key.

Gotcha: Local Scope Leaks

Global scopes defined in booted() apply to ALL queries on that model — including relationships. An innocent User::all() in admin panel might exclude soft-deleted users if a global scope is active.

Senior Insight

Artisan command output formatting is underappreciated. I've built CLI tools that developers use daily, and the difference between a well-formatted table output and a raw dump() is the difference between a tool people love and one they avoid. Use Table, ProgressBar, and choice() questions liberally. A developer who can run php artisan reports:daily and get a formatted table of key metrics will actually use it.

Source: Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/laravel), Spatie Blog (https://spatie.be/blog)

────────────────────────────────────────────────────────
<-- back to posts