Master the Artisan CLI to automate repetitive tasks and debug your Laravel application. Learn how to list, create, and run custom commands today.
Previously in this course, we explored Task Manager: Adding Status and Priorities in Laravel, where we evolved our database schema to handle more complex data. Now, we shift our focus from the browser to the terminal, where you'll learn to harness the full power of Artisan to speed up your development workflow.
Artisan is the command-line interface (CLI) included with Laravel. It provides a number of helpful commands for your application, ranging from scaffolding code to managing database migrations and clearing caches. Think of it as your primary bridge between your code and the underlying framework.
When you run php artisan in your project root, you aren't just running a script; you are interacting with a sophisticated tool designed to handle the "heavy lifting" of application management.
To see everything Artisan can do, open your terminal in your project directory and run:
Bashphp artisan
You will see a list of commands categorized by their function. Common ones you’ve already used include make:migration or make:controller. If you ever need to know the specific options for a command, you can use the help flag:
Bashphp artisan help make:controller
This is your go-to reference whenever you forget the exact syntax of a flag or argument.
While built-in commands are powerful, you will eventually reach a point where you need to perform custom tasks—like cleaning up old tasks in our Task Manager app or sending a daily summary email. Artisan makes this easy.
To create a custom command, run the following:
Bashphp artisan make:command CleanupOldTasks
This generates a file in app/Console/Commands. Open that file, and you'll see a $signature property. This is what you type into the CLI to trigger your code.
PHPprotected $signature = 'tasks:cleanup'; public function handle() { $count = \App\Models\Task::where('created_at', '<', now()->subDays(30))->delete(); $this->info("Deleted {$count} old tasks."); }
In this example, we’ve created a command that deletes tasks older than 30 days. The handle() method contains the logic that runs when you execute php artisan tasks:cleanup. For more advanced implementations, refer to Laravel Artisan Custom Commands: Automate Tasks Like a Pro and Mastering Laravel Artisan Command Development for CLI Tools.
Sometimes, you don't want to write a full command or a controller method just to test a small snippet of code. This is where php artisan tinker shines.
Tinker is a REPL (Read-Eval-Print Loop) for your Laravel application. It allows you to interact with your database and application logic in real-time.
php artisan tinker.PHP\App\Models\Task::create(['title' => 'Tinker Test', 'user_id' => 1]);
PHP\App\Models\Task::all();
It’s an incredibly fast way to verify your Introduction to Database Relationships in Laravel or debug why a query isn't returning what you expect.
For our Task Manager project, create a custom command called tasks:status-report that prints the total number of tasks currently in the database to the terminal. Use $this->info() to output the result. Once finished, run it and verify the count matches your database expectations.
app/Console/Commands folder. If your command isn't showing up, ensure the file is in that directory and the namespace is correct.handle(): Keep your logic inside handle() thin. If your command does something complex, extract that logic into a service class.exit or Ctrl+C) for the changes to take effect.Artisan is more than just a list of commands; it is the engine of your development environment. By leveraging CLI automation, you minimize manual errors and speed up your workflow. You now know how to list commands, build your own, and use Tinker to inspect your application's state on the fly.
Up next: We will dive deeper into the power of the REPL in "Debugging with Laravel Tinker."
Laravel Artisan custom console commands help you automate repetitive tasks easily. Learn to build your own CLI tools to boost your development productivity.
Introduction to Artisan Commands
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation