What is Artisan in Laravel?
Artisan is Laravel’s built-in command-line interface (CLI), designed to automate repetitive tasks, manage databases, generate boilerplate code, and interact with your application efficiently. It exists as a script at the root of your project (php artisan), empowering developers to work faster and more systematically.
Why Artisan Matters
Without Artisan, tasks like creating controllers, migrations, or scheduling jobs would require manual coding. Artisan saves time, reduces errors, and improves maintainability. Modern Laravel versions (v11 and v12) also include interactive forms and prompts, enhancing usability for developers.
Modern Key Features
- Code Generation: Scaffold models, controllers, migrations, factories, and seeders with commands like
php artisan make:model Post -mfs. - Database Management: Run
php artisan migratefor schema changes andphp artisan db:seedfor populating data. - REPL (Tinker): Open an interactive shell with
php artisan tinkerto experiment with Eloquent models, jobs, and events. - Laravel Prompts (v11/v12): Modern, user-friendly forms with validation, placeholders, and multi-select options for gathering input interactively.
- Task Scheduling: Use
php artisan schedule:workorschedule:runto handle cron jobs entirely within Laravel. - Closure-Based Commands: Define quick commands directly in
routes/console.phpwithout creating full classes. - Stub Customization: Customize the code templates generated by Artisan to match your project standards.
Example 1: Interactive User Input (Laravel Prompts)
use function Laravel\Prompts\text;
use function Laravel\Prompts\password;
use function Laravel\Prompts\confirm;
public function handle()
{
$name = text('What is your name?', required: true);
$pass = password('Enter your API secret');
if (confirm('Do you want to run the migration?')) {
$this->info("Proceeding, $name...");
}
}Example 2: Closure-Based Command
use Illuminate\Support\Facades\Artisan;
Artisan::command('report:generate {type}', function (string $type) {
$this->info("Generating the {$type} report...");
})->purpose('Generate a daily business report');Example 3: Custom Command with Dependency Injection
namespace App\Console\Commands;
use App\Models\User;
use App\Support\DripEmailer;
use Illuminate\Console\Command;
class SendEmails extends Command
{
protected $signature = 'mail:send {user}';
protected $description = 'Send a marketing email to a user';
public function handle(DripEmailer $drip)
{
$drip->send(User::find($this->argument('user')));
$this->info('Email sent successfully!');
}
}Example 4: Stub Customization
To modify the templates used for generating controllers, models, or migrations:
php artisan stub:publishAll default stubs are copied to /stubs so you can edit them. New files generated with make:controller, make:model, etc., will now use your customized templates.
Essential Artisan Command Cheat Sheet
| Command | Description |
|---|---|
| php artisan list | View all commands available in the application |
| php artisan about | Show a detailed overview of your environment and dependencies |
| php artisan make:… | Create controllers, models, events, mail classes, and more |
| php artisan migrate | Run pending database migrations |
| php artisan db:seed | Populate database with seed data |
| php artisan route:list | View all registered routes and their controllers |
| php artisan optimize | Cache routes, config, and views for production |
Conclusion: Artisan is no longer just a basic CLI; it’s a fully featured developer toolkit in Laravel 11 and 12. With modern features like Laravel Prompts, closure commands, and stub customization, developers can automate, test, and scaffold entire applications with efficiency and elegance.
Related Answers
Still need help?
Talk to our Laravel experts
We've handled GDPR/CCPA compliance for dozens of EU & US Laravel.
