📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Laravel Framework Custom Artisan Commands

Custom Artisan Commands

6 min read
Build interactive Artisan commands with arguments, options, progress bars, and table output.

Custom Artisan Commands

php artisan make:command SendDailyReport

class SendDailyReport extends Command {
    protected $signature = "report:daily
                            {--date= : Report date (Y-m-d)}
                            {--email= : Send to email}
                            {type : Type of report}";

    protected $description = "Send the daily report email";

    public function handle(): int {
        $type  = $this->argument("type");
        $date  = $this->option("date") ?? today()->format("Y-m-d");
        $email = $this->option("email");

        $this->info("Generating $type report for $date...");

        $bar = $this->output->createProgressBar(100);
        foreach (range(1, 100) as $i) {
            // do work
            $bar->advance();
        }
        $bar->finish();

        $this->line("");
        $this->newLine();
        $this->table(["Field", "Value"], [["Date", $date], ["Type", $type]]);

        $this->info("Done!");
        return Command::SUCCESS;
    }
}