Introduction to Artisan Commands: Your CLI Power Tool
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.
What is Artisan?
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.
Listing and Using Commands
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.
Creating Custom Commands for Automation
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.
Debugging with Tinker
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.
- Run
php artisan tinker. - Try creating a new task:
PHP
\App\Models\Task::create(['title' => 'Tinker Test', 'user_id' => 1]); - Query it back:
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.
Hands-on Exercise
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.
Common Pitfalls
- Forgetting to Register: In older versions of Laravel, you had to manually register commands. In modern Laravel, the framework auto-discovers them in the
app/Console/Commandsfolder. If your command isn't showing up, ensure the file is in that directory and the namespace is correct. - Over-complicating
handle(): Keep your logic insidehandle()thin. If your command does something complex, extract that logic into a service class. - Tinker State: Remember that Tinker keeps your application state in memory. If you change a file on disk, you may need to exit and restart Tinker (
exitorCtrl+C) for the changes to take effect.
Recap
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."
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

FilamentPHP Admin Panel & Dashboard Development
A powerful admin panel for your Laravel app — built with FilamentPHP so you can manage everything without touching the database.