Artisan CLI
Artisan is Laravel's command-line tool for generating code and running tasks.
Artisan is the command-line interface included with Laravel. You run it with php artisan from your project root. It can scaffold classes, run migrations, clear caches and more.
Common commands
| Command | What it does |
|---|---|
php artisan serve | Start a local dev server |
php artisan make:controller | Create a controller |
php artisan make:model | Create an Eloquent model |
php artisan migrate | Run database migrations |
php artisan route:list | Show all registered routes |
Example
// Run these in your terminal, not in a script:
// php artisan serve
// php artisan make:model Post -mWhen to use it
- A developer runs 'php artisan make:model Product -mcr' to generate a model, migration, and resource controller for a new feature in a single command.
- A DevOps engineer adds 'php artisan migrate --force' to the deployment script so database schema changes are applied automatically on every production release.
- A team writes a custom Artisan command to import a CSV of products into the database, allowing non-technical staff to trigger the import safely from the CLI.
More examples
List and run built-in commands
Shows the three most common day-to-day Artisan workflows: discovery, code generation, and database migration.
# List every available command
php artisan list
# Generate a new controller
php artisan make:controller ProductController --resource
# Run pending migrations
php artisan migrateCreate a custom Artisan command
Defines a custom command with an optional --dry-run flag, demonstrating signature syntax, option reading, and output helpers.
class SendWeeklyReport extends Command
{
protected $signature = 'report:weekly {--dry-run}';
protected $description = 'Email the weekly sales report';
public function handle(): void
{
if ($this->option('dry-run')) {
$this->info('Dry run -- no email sent.');
return;
}
Mail::to('[email protected]')->send(new WeeklyReportMail());
$this->info('Report sent.');
}
}Schedule a command in the kernel
Registers the custom command in the scheduler so it runs every Monday at 08:00 without requiring a separate cron entry per command.
// app/Console/Kernel.php
protected function schedule(Schedule $schedule): void
{
$schedule->command('report:weekly')->weeklyOn(1, '08:00');
$schedule->command('inspire')->hourly();
}
Discussion