Comprehensive Guide to Laravel Artisan Commands

1 Oct

Laravel’s Artisan command-line interface (CLI) is a powerful tool that simplifies many common tasks in a Laravel application. This guide will cover the most useful Artisan commands, providing a comprehensive reference for developers. We’ll delve into commands related to maintenance, environment management, migrations, caching, configuration, database seeding, event handling, key generation, making various components, and more. Each command helps streamline your workflow, making Laravel development more efficient.

Maintenance and Environment Commands

clear-compiled

This command removes the compiled class file, which can help resolve issues with cached classes.

php artisan clear-compiled

down

Put the application into maintenance mode. This is useful during deployments or critical updates.

php artisan down

env

Display the current framework environment, such as local, production, or testing.

php artisan env

help

Displays help for a specific command.

php artisan help migrate

inspire

Display an inspiring quote. This command can lighten the mood during long coding sessions.

php artisan inspire

list

Lists all available Artisan commands.

php artisan list

serve

Serve the application on the PHP development server.

php artisan serve

tinker

Interact with your application using the REPL (Read-Eval-Print Loop).

php artisan tinker

up

Bring the application out of maintenance mode.

php artisan up

Authentication Commands

auth

Flush expired password reset tokens.

php artisan auth:clear-resets

Cache Commands

cache

Flush the application cache.

php artisan cache:clear

cache

Remove an item from the cache.

php artisan cache:forget {key}

cache

Create a migration for the cache database table

php artisan cache:table

Configuration Commands

config

Create a cache file for faster configuration loading.

php artisan config:cache

config

Remove the configuration cache file.

php artisan config:clear

Database Commands

db:seed

Seed the database with records.

php artisan db:seed

db:wipe

Drop all tables, views, and types.

php artisan db:wipe

Event Commands

event:cache

Discover and cache the application’s events and listeners.

php artisan event:cache

event:clear

Clear all cached events and listeners.

php artisan event:clear

event:generate

Generate the missing events and listeners based on registration.

php artisan event:generate

event:list

List the application’s events and listeners.

php artisan event:list

Flare Commands

flare:test

Send a test notification to Flare.

php artisan flare:test

Key Generation Command

key:generate

Set the application key, which is used for encryption and should be unique.

php artisan key:generate

Make Commands

The make command is used to create various components in a Laravel application. Here are some of the most common make commands:

make:channel

Create a new channel class.

php artisan make:channel ChannelName

make:command

Create a new Artisan command.

php artisan make:command CommandName

make:controller

Create a new controller class.

php artisan make:controller ControllerName

make:event

Create a new event class.

php artisan make:event EventName

make:exception

Create a new custom exception class.

php artisan make:exception ExceptionName

make:factory

Create a new model factory.

php artisan make:factory FactoryName

make:job

Create a new job class.

php artisan make:job JobName

make:listener

Create a new event listener class.

php artisan make:listener ListenerName

make:mail

Create a new email class.

php artisan make:mail MailName

make:middleware

Create a new middleware class.

php artisan make:middleware MiddlewareName

make:migration

Create a new migration file.

php artisan make:migration migration_name

make:model

Create a new Eloquent model class.

php artisan make:model ModelName

make:notification

Create a new notification class.

php artisan make:notification NotificationName

make:observer

Create a new observer class.

php artisan make:observer ObserverName

make:policy

Create a new policy class.

php artisan make:policy PolicyName

make:provider

Create a new service provider class.

php artisan make:provider ProviderName

make:request

Create a new form request class.

php artisan make:request RequestName

make:resource

Create a new resource.

php artisan make:resource ResourceName

make:rule

Create a new validation rule.\

php artisan make:rule RuleName

make:seeder

Create a new seeder class.

php artisan make:seeder SeederName

make:test

Create a new test class.

php artisan make:test TestName

Migration Commands

migrate

Run the database migrations.

php artisan migrate

migrate:fresh

Drop all tables and re-run all migrations.

php artisan migrate:fresh

migrate:install

Create the migration repository.

php artisan migrate:install

migrate:refresh

Reset and re-run all migrations.

php artisan migrate:refresh

migrate:reset

Rollback all database migrations.

php artisan migrate:reset

migrate:rollback

Rollback the last database migration.

php artisan migrate:rollback

migrate:status

Show the status of each migration.

php artisan migrate:status

Notifications Commands

notifications

Create a migration for the notifications table.

php artisan notifications:table

Optimization Commands

optimize

Cache the framework bootstrap files.

php artisan optimize

optimize:clear

Remove the cached bootstrap files.

php artisan optimize:clear

Package Commands

package:discover

Rebuild the cached package manifest.

php artisan package:discover

Queue Commands

queue:failed

List all of the failed queue jobs.

php artisan queue:failed

queue:failed-table

Create a migration for the failed queue jobs database table.

php artisan queue:failed-table

queue:flush

Flush all of the failed queue jobs.

php artisan queue:flush

queue:forget {id}

Delete a failed queue job.

php artisan queue:forget {id}

queue:listen

Listen to a given queue.

php artisan queue:listen

queue:restart

Restart queue worker daemons after their current job.

php artisan queue:restart

php artisan queue:retry {id}

Retry a failed queue job.

php artisan queue:retry {id}

queue:table

Create a migration for the queue jobs database table.

php artisan queue:table

queue:work

Start processing jobs on the queue as a daemon.

php artisan queue:work

Route Commands

route:cache

Create a route cache file for faster route registration.

php artisan route:cache

route:clear

Remove the route cache file.

php artisan route:clear

route:list

List all registered routes.

php artisan route:list

Schedule Commands

schedule

Run the scheduled commands.

php artisan schedule:run

Session Commands

session:table

Create a migration for the session database table.

php artisan session:table

Storage Commands

storage

Create a symbolic link from “public/storage” to “storage/app/public”.

php artisan storage:link

Vendor Commands

vendor

Publish any publishable assets from vendor packages.

php artisan vendor:publish

View Commands

view:cache

Compile all of the application’s Blade templates.

php artisan view:cache

view:clear

Clear all compiled view files.

php artisan view:clear

Conclusion

Laravel Artisan commands are incredibly powerful and versatile, helping you manage every aspect of your Laravel application with ease. Whether you’re migrating databases, caching configurations, or creating new classes, these commands streamline your development process. Keep this guide handy as a reference to make the most out of Artisan and enhance your productivity in Laravel development.