The foundation of productivity in the Laravel universe is the artisan command-line interface. It’s the potent tool that distinguishes a good workflow from a great one, automating the tedious chores that impede our progress and freeing us up to concentrate on what really counts: creating amazing applications.
But where do you even begin with hundreds of commands?
The best resource for you is this guide. The breadth of Artisan has been condensed into a useful, arranged list of the commands you’ll really use on a daily basis. This is the resource you should use to get proficient with the Laravel CLI, whether you’re scaffolding a new feature or deploying to production.
Project & Environment Management
These are your foundational commands, used for creating projects, running a local server, and managing your application’s state.
Command | Description |
laravel new project-name | The quickest method for starting a new Laravel project is via Laravel Installer, which requires the global Laravel/installer package. |
php artisan serve | Starts a lightweight, local development server, typically at http://127.0.0.1:8000. Essential for daily development. |
php artisan list | Displays a complete list of all available Artisan commands within your project. A great tool for discovery. |
php artisan –version | Quickly checks the version of the Laravel framework your project is running. |
php artisan tinker | This is an essential tool that allows you to run any PHP or Laravel code directly by launching an interactive shell (REPL) that bootstraps your entire application. It’s ideal for testing database queries, triggering events, or debugging logic in real time. |
php artisan down | switches the program into “Maintenance Mode.” A 503 error page will be displayed for every request, enabling you to make adjustments securely.A pro advice is to use flags like –secret and –redirect to gain more control. |
php artisan up | Brings your application back online and out of maintenance mode. |
The make commands
The make commands are the heart of Artisan’s time-saving magic. Never create a boilerplate file by hand again.
Command | Description |
php artisan make:model Post | Creates a new Eloquent model. Pro-tip: Chain flags for maximum efficiency: make:model Post -mfs will also create a migration, factory, and seeder for the model. |
php artisan make:controller PostController | Creates a new controller. Pro-tip: Use –resource to generate a controller with stubs for all resourceful actions (index, create, store, show, edit, update, destroy). |
php artisan make:migration create_posts_table | Creates a new database migration file. The descriptive name helps keep your schema history organized. |
php artisan make:request StorePostRequest | Generates a Form Request class. This is the best practice for encapsulating complex validation and authorization logic away from your controller. |
php artisan make:factory PostFactory | Creates a model factory, which is essential for generating fake data for testing and database seeding. |
php artisan make:middleware CheckAdmin | Creates a new middleware class to filter HTTP requests. |
php artisan make:job ProcessPodcast | Creates a new job class that can be dispatched and processed asynchronously in a queue. |
php artisan make:notification InvoicePaid | Creates a new notification class for sending messages via channels like email, Slack, or SMS. |
php artisan make:event OrderShipped | Generates an event class that can be dispatched when something notable happens in your app. |
php artisan make:listener SendShipmentNotification | Generates a listener class that subscribes to a specific event. |
php artisan make:policy PostPolicy | Creates a policy class to organize authorization logic around a specific model or resource. |
php artisan make:rule Isbn | Creates a powerful, reusable custom validation rule class. |
To create model, migration and controller at a time, you can use following command:
php artisan make:model ModelName –migration –Controller –resource
e.g :- php artisan make:model Task –migration –controller –resource
OR
php artisan make:model Task —all : This command will create model, migration, controller, factory, seeder, policy, and form request.
Create a Model, Migration, Factory, and a Resource Controller:
This is perfect for building a new CRUD feature from scratch.
php artisan make:model Invoice -mfcr
Database & Migrations
Manage your database schema, roll back changes, and populate your tables with ease.
Command | Description |
php artisan migrate | Executes all of your “up” migrations that have not yet been run, bringing your database schema up to date. |
php artisan migrate:rollback | Reverts the last batch of migrations. Useful for quickly undoing a recent change. |
php artisan migrate:fresh | Drops all tables from the database and then runs all migrations from the beginning. Perfect for resetting your database during development. |
php artisan db:seed | Runs your database seeder classes to populate your tables with data. |
php artisan migrate:fresh –seed | A developer’s best friend. This single command drops all tables, re-migrates the schema, and seeds the database. The ultimate clean slate. |
php artisan db:wipe | Drops all tables, views, and types from the database. Faster than migrate:fresh if you just want an empty database. |
Testing Your Application
Laravel’s testing tools are first-class, and Artisan provides the entry point to run your test suites.
Command | Description |
php artisan test | The main command to run your entire test suite (both Feature and Unit tests). |
php artisan test –filter=UserTest | Runs only the tests within a specific file or that match a method name. Invaluable for focusing on a single feature. |
php artisan test –group=slow | Runs only tests that have the @group slow annotation in their docblock. Great for separating slow, browser-based tests from fast unit tests. |
Optimization for Production
These commands should only be run as part of your deployment process. They combine files into a single cached version, and running them in development can lead to frustrating issues where your changes don’t appear.
Command | Description |
php artisan optimize | A powerful command that caches your configuration and routes files. This significantly improves performance. |
php artisan config:cache | Caches your application’s configuration into a single file for faster loading. |
php artisan route:cache | Caches your route declarations, dramatically speeding up route registration on every request. |
php artisan view:cache | Pre-compiles all of your Blade views so they don’t have to be re-compiled on the fly. |
php artisan optimize:clear | The antidote. This command clears all the caches created by the commands above. Run this if you ever encounter issues after deploying. |
php artisan key:generate | Generates and sets a secure APP_KEY in your .env file. This is one of the very first commands you should run in any new project. |
More than just a tool, Artisan is a fundamental component of the Laravel philosophy, which aims to make development fun and efficient. You can write code more quickly, maintain your apps more easily, and take advantage of the Laravel framework’s full potential by incorporating these commands into your daily workflow.