Laravel Artisan Commands to Know and How to Create Them

Laravel Artisan command

Laravel has become a go-to PHP framework for crafting modern web applications. One of the features that really sets it apart is the Artisan CLI (Command Line Interface). This handy tool comes packed with powerful built-in commands that make development tasks a breeze, like handling database migrations, scaffolding code, and running tests. Plus, Laravel allows you to whip up your own custom commands, making it super easy to automate those repetitive tasks.

In this blog, we’re going to dive into the essential Artisan commands that every Laravel developer should be familiar with. Plus, we’ll walk you through a detailed guide on how to create your own custom Artisan commands.

Here is a comprehensive list of the most commonly used Laravel Artisan commands, organized by category:

1. Help & General Commands

1. Lists all available Artisan commands

php artisan list

2. Displays help information about a specific command

php artisan help {command}

3. Displays Laravel version

php artisan --version

2. Make (Code Generation) Commands

1. Runs pending database migrations

php artisan make:controller {ControllerName}

2. Creates a new Eloquent model 

php artisan make:model {ModelName}

3.  Creates a model and its migration

php artisan make:model {ModelName} -m

4. Creates a new migration file

php artisan make:migration {MigrationName}

5. Creates a new database seeder

php artisan make:seeder {SeederName}

6. Creates a model factory

php artisan make:factory {FactoryName}

7. Creates a new middleware class

php artisan make:middleware {MiddlewareName}

8. Creates a custom Artisan command

php artisan make:command {CommandName}

9. Creates an authorization policy

php artisan make:policy {PolicyName}

10. Creates a form request class

php artisan make:request {RequestName}

11. Creates a new event class

php artisan make:event {EventName}

12. Creates an event listener class

php artisan make:listener {ListenerName}

13. Creates a new job class 

php artisan make:job {JobName}

14. Creates a new notification class

php artisan make:notification {NotificationName}

15. Creates a PHPUnit test class

php artisan make:test {TestName}

3. Database Commands

1. Runs pending database migrations

php artisan migrate

2. Rolls back the last migration batch

php artisan migrate:rollback

3. Rolls back all migrations

php artisan migrate:reset

4. Resets and re-runs all migrations

php artisan migrate:refresh

5. Drops all tables and runs all migrations afresh

php artisan migrate:fresh

6.  Runs database seeders

php artisan db:seed

7. Runs a specific seeder class

php artisan db:seed --class={SeederClass}

8. Displays the status of each migration

php artisan migrate:status

4. Cache & Configuration

1. Caches the configuration files

php artisan config:cache

2. Clears the configuration cache

php artisan config:clear

3. Clears the application cache

php artisan cache:clear

4. Caches the routes for performance

php artisan route:cache

5. Clears the route cache

php artisan route:clear

6. Clears the compiled view files

php artisan view:clear

7. Optimizes the framework for better performance (deprecated in recent versions)

php artisan optimize

5. Routes

1. Lists all registered routes

php artisan route:list

2. Clears cached routes

php artisan route:clear

3. Caches all routes

php artisan route:cache

6. Queue

1. Processes jobs in the queue

php artisan queue:work

2. Listens to the queue and processes jobs as they come

php artisan queue:listen

3. Retry a failed queued job

php artisan queue:retry {id}

4. Lists all failed jobs

php artisan queue:failed

5. Deletes all failed jobs

php artisan queue:flush

7. Scheduled Tasks

1. Executes scheduled tasks defined in app/Console/Kernel.php

php artisan schedule:run

2. Lists scheduled commands (Laravel 9+)

php artisan schedule:list

8. Environment

1. Displays the current application environment

php artisan env

2. Generates a new application key and updates .env

php artisan key:generate

9. Testing

Runs all PHPUnit tests

php artisan test

10. Custom Artisan Commands

Once created using php artisan make:command {CommandName}, you can run your custom command like this:

php artisan custom:task

(Custom commands are registered in app/Console/Commands/)

Bonus: Tinker (Interactive Console)

Opens an interactive REPL to interact with your application

php artisan tinker


Laravel Artisan commands are a developer’s best friend. They help with generating boilerplate code, handling database migrations, clearing cache, and running custom scripts. Artisan saves time and improves workflow consistency.

Use php artisan list at any time to see all the available commands for your specific Laravel version.

No comments:

Post a Comment

Python in AI: Getting Started with TensorFlow & PyTorch

  Artificial Intelligence (AI) is no longer just a buzzword. It powers recommendation systems, chatbots, self-driving cars, and more. At the...