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

CommandWhat it does
php artisan serveStart a local dev server
php artisan make:controllerCreate a controller
php artisan make:modelCreate an Eloquent model
php artisan migrateRun database migrations
php artisan route:listShow all registered routes

Example

Example · php
// Run these in your terminal, not in a script:
// php artisan serve
// php artisan make:model Post -m

When 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.

Example · bash
# List every available command
php artisan list

# Generate a new controller
php artisan make:controller ProductController --resource

# Run pending migrations
php artisan migrate

Create a custom Artisan command

Defines a custom command with an optional --dry-run flag, demonstrating signature syntax, option reading, and output helpers.

Example · php
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.

Example · php
// app/Console/Kernel.php
protected function schedule(Schedule $schedule): void
{
    $schedule->command('report:weekly')->weeklyOn(1, '08:00');
    $schedule->command('inspire')->hourly();
}

Discussion

  • Be the first to comment on this lesson.