Artisan commands

Contents
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

Related articles
Development with PHP
What is Laravel and where it runs
Install Laravel
Your first route
Controllers
Blade basics
Blade advanced
Migrations
Eloquent models
Relationships
Query builder
Seeders and factories
Validation
Auth intro
Hand-rolled login
Middleware
Service container
Config and env
Artisan commands
Events and listeners
Queues
Mail
Cache
Sessions in Laravel
CSRF
Hashing
Sanctum tokens
API resources
Gates and policies
Notifications
Scheduler
Filesystem
Localization
Testing
HTTP tests
Collections
Carbon dates
Pagination
File uploads
Eloquent advanced
Transactions and locks
Deployment
Debugbar and dd
Broadcasting
Full-text search
Capstone: blog
Capstone: JSON API
Capstone: shop cart
Performance: kill N+1
Modern Laravel tour
Hero roadmap: the whole course on one page
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: