Artisan commands
| Make a command | |
| Signature | |
| Handle logic | |
| Run it |
Make a command
Artisan commands wrap maintenance work such as reports or cleanup into named terminal tools. Create a new command class with the canonical maker command.
php artisan make:command SendReport
INFO Console command created successfully.
Signature
The signature property names the command and its options, while the description property documents it for the command list. Laravel reads both without any extra attribute setup.
Keep names in lowercase with dashes and colons so they read well in the terminal.
protected $signature = "report:send {--queue}"; protected $description = "Send the weekly report";
Handle logic
The handle method holds the actual work and runs when the command is called. Helper methods such as info print progress, and the returned integer becomes the exit code.
Return zero on success and a nonzero code on failure so scripts and schedulers can react.
public function handle(): int { $this->info("Report sent"); return 0; }
Run it
Run the finished command by its signature from the project root. Add option flags from the signature after the name when needed.
php artisan report:send
Report sent.
Next: Events and listeners
Article author: Arthur Isaev