Laravel - Artisan Console - Progress Bars
For long running tasks, it can be helpful to show a progress bar that informs users how complete the task is. Using the withProgressBar
method, Laravel will display a progress bar and advance its progress for each iteration over a given iterable value:
use App\Models\User;
$users = $this->withProgressBar(User::all(), function ($user) {
$this->performTask($user);
});
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 after processing each item:
$users = App\Models\User::all();
$bar = $this->output->createProgressBar(count($users));
$bar->start();
foreach ($users as $user) {
$this->performTask($user);
$bar->advance();
}
$bar->finish();
For more advanced options, check out the Symfony Progress Bar component documentation.