Laravel Artisan Console: Common Commands
Artisan is Laravel's built-in command-line interface, and it's one of the framework's most time-saving tools. Instead of manually creating boilerplate files or running raw database commands, Artisan automates the repetitive parts of development.
What Is Artisan?
Every Laravel project ships with an artisan script in its root folder. Running it through PHP gives access to dozens of built-in commands, and custom commands can be added for project-specific tasks like data imports or scheduled maintenance.
Frequently Used Commands
| Command | What It Does |
|---|---|
php artisan serve | Starts a local development server |
php artisan make:model | Generates a new Eloquent model class |
php artisan make:controller | Generates a new controller class |
php artisan migrate | Runs pending database migrations |
php artisan route:list | Displays every registered route in the application |
php artisan tinker | Opens an interactive shell for testing code directly |
Why It Matters
- Reduces manual file creation and copy-paste errors
- Keeps generated files consistent with Laravel's naming conventions
- Speeds up debugging through tools like Tinker and route listing
- Handles maintenance tasks like clearing caches or queueing jobs
Many Artisan commands, especially those touching the database, rely on settings defined in one central place — the .env file — which is exactly what we cover next.
Generators That Save the Most Time
The make: family accepts flags that create several related files in one go. Learning a few of them removes a lot of repetitive setup.
php artisan make:model Post -mcr # model + migration + resource controller
php artisan make:model Order -a # model with migration, factory, seeder, policy, controller and form requests
php artisan make:request StorePostRequest
php artisan make:middleware EnsureUserIsActive
php artisan list make # every generator available in your version
If you forget what a command accepts, php artisan help migrate prints its arguments and options. php artisan about shows the environment, cache status and drivers in use, which is handy when something behaves differently on two machines.
Database and Cache Commands
| Command | Use it when |
|---|---|
php artisan migrate:status | You want to see which migrations have run |
php artisan migrate:rollback | You need to undo the last batch of migrations |
php artisan migrate:fresh --seed | You want a clean local database with sample data |
php artisan db:seed | You only want to run seeders |
php artisan optimize:clear | Config, route or view caches seem stale |
php artisan config:cache | You are deploying and want faster config loading |
php artisan down / up | You need maintenance mode during a deployment |
php artisan queue:work | You need a worker to process queued jobs |
Be careful: migrate:fresh drops every table before re-running migrations. It is perfect on a laptop and destructive on a live server. On production, prefer plain migrate with the --force flag, and take a database backup first.
Writing Your Own Command
Custom commands are ideal for jobs you run repeatedly, such as sending a report or cleaning old records. Generate the class first:
php artisan make:command PruneOldDrafts
Then fill in the signature and the handle() method in app/Console/Commands/PruneOldDrafts.php:
class PruneOldDrafts extends Command
{
protected $signature = 'drafts:prune {--days=30 : Delete drafts older than this}';
protected $description = 'Delete unpublished drafts older than the given number of days';
public function handle(): int
{
$days = (int) $this->option('days');
$count = Post::where('status', 'draft')
->where('updated_at', '<', now()->subDays($days))
->delete();
$this->info("Deleted {$count} old drafts.");
return self::SUCCESS;
}
}
The signature does two jobs: it names the command and declares its inputs. Text in curly braces without dashes is a required argument, while --days=30 defines an option with a default value. The text after the colon becomes the help description shown by php artisan help drafts:prune.
Run it with php artisan drafts:prune --days=60. Recent Laravel versions discover commands in that folder automatically, and the same command can be added to the scheduler so it runs on its own.
Common Mistakes With Artisan
- Running
config:cacheand then callingenv()outside config files; cached config means those calls returnnull. - Forgetting to clear caches after changing routes or
.envvalues on a server. - Running Artisan with a different PHP version than the web server uses.
- Putting heavy logic inside the command instead of a service class that can also be tested and reused.
Practical habit: keep a short list of the commands your project uses for setup, testing and deployment in the README so every developer runs them the same way.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.