Laravel Artisan Commands

Laravel Artisan is the command-line interface included with every Laravel app. Artisan commands generate code, run migrations, clear caches, start the queue worker and much more. You can also write your own commands for automation.

Most Useful Artisan Commands

  • php artisan list: show every command
  • php artisan make:model / make:controller / make:migration / make:request: generate code
  • php artisan migrate and db:seed: manage the database
  • php artisan route:list: see all routes
  • php artisan tinker: interactive PHP shell with your app loaded
  • php artisan optimize: cache config, routes, events and views for production
  • php artisan optimize:clear: clear all of those caches
  • php artisan down / up: maintenance mode

Exploring Data With Tinker

Tinker is a REPL where you can run Eloquent queries and test code against your real app.

php artisan tinker
> User::count()
> User::where('email', 'like', '%@example.com')->pluck('name')
> Str::slug('Hello Laravel Artisan')
= 5
= Illuminate\Support\Collection {#6123
    all: ["Naeem", "Sara"],
  }
= "hello-laravel-artisan"

Creating a Custom Artisan Command

php artisan make:command SendDailyReport

<?php
namespace App\Console\Commands;

use App\Models\Order;
use Illuminate\Console\Command;

class SendDailyReport extends Command
{
    protected $signature = 'report:daily {--date= : Day to report on (Y-m-d)}';
    protected $description = 'Summarise orders for a given day';

    public function handle(): int
    {
        $date = $this->option('date') ?? now()->toDateString();
        $orders = Order::whereDate('created_at', $date)->get();

        $this->info("Orders on $date: {$orders->count()}");
        $this->table(['ID', 'Total'], $orders->map(fn ($o) => [$o->id, $o->total]));

        return self::SUCCESS;
    }
}
$ php artisan report:daily --date=2026-10-30
Orders on 2026-10-30: 2
+-----+--------+
| ID  | Total  |
+-----+--------+
| 101 | 49.90  |
| 102 | 120.00 |
+-----+--------+

Closure Commands

Small commands can be defined directly in routes/console.php without a class.

<?php
use Illuminate\Support\Facades\Artisan;

Artisan::command('greet {name}', function (string $name) {
    $this->comment("Assalam-o-Alaikum, $name!");
})->purpose('Greet someone');
$ php artisan greet Naeem
Assalam-o-Alaikum, Naeem!

Production Tips

On deployment, run php artisan migrate --force and php artisan optimize. If configuration changes do not seem to apply, you probably have a cached config: run php artisan optimize:clear. Never call env() outside config files, because it returns null once config is cached.

Frequently asked questions

How do I clear cache in Laravel?

Run php artisan optimize:clear to clear config, route, view and application caches at once, or cache:clear for just the application cache.

What is Laravel Tinker used for?

Tinker is an interactive shell for running PHP and Eloquent code against your application, useful for quick experiments and debugging data.

How do I run an Artisan command from code?

Use Artisan::call("report:daily", ["--date" => "2026-10-30"]) or schedule it in routes/console.php.