There are a few things I make sure I do when creating custom artisan commands. This includes:

  • Notifying that the script has started.
  • Show it’s current progress.
  • Notifying when the script has ended.

I find it important to keep whoever is running the script notified if I were to run the artisan command manually (such as a fix for DB records).

Artisan scripts have a handy $this->output->createProgressBar($count) method to display the progress of an operation within a script.

If iterating through a collection, I tend to set this within the ->tap() collection method to initialize the progress bar based on the sum of records within the collection:

...

protected $bar;

...

$this->info('*** Starting script ***');

Comment::all()
   ->tap(function ($collection) {
       $this->bar = $this->output->createProgressBar($collection->count());
   })

...

Now, when processing each record, you can update bar using:

$this->bar->advance();

I normally to set this as a class property as then I dont need to add via a use() statement on a closure call and reference the variable.

After everything is done, I make sure to finish up the my use of the progress bar by using:

$this->bar->finish();

$this->info("\n Completed");

Hope this helps. Till next time.