Usefull Laravel Artisan Commands with Examples
Read More
Laravel Artisan is a powerful command-line tool that makes managing and developing Laravel applications efficient and enjoyable. With Artisan, you can create models, run migrations, manage databases, clear caches, and much more—all with a few simple commands. In this guide, we’ll walk you through the most essential Artisan commands, complete with examples to help you along the way.
Starting a Laravel project from scratch? Here’s how to get your application up and running.
The first step in building a Laravel project is creating the project itself. Use this command:
laravel new ProjectName
Example: Starting a new project called MyApp
.
laravel new MyApp
Once your project is created, you can run it locally:
php artisan serve
Example: Start your Laravel application on port 8000.
php artisan serve --port=8000
Before diving into development, it's helpful to clear caches and optimize configurations for faster response times.
When working with cached configurations, routes, and views, this command will clear all cache files in one go:
php artisan optimize:clear
To cache configuration and route files for faster performance, use:
php artisan optimize
php artisan make:model ModelName -mcr
Artisan makes it simple to generate the files you need to interact with your database and control your app’s logic.
When creating a new model, you can also create its migration file, a controller, and a resource in one command:
php artisan make:model ModelName -mcr
Example: Create a Product
model with a migration file, resource controller, and resource.
php artisan make:model Product -mcr
Factories allow you to seed your database with dummy data, perfect for testing.
php artisan make:factory FactoryName
Example: Create a factory for the Product
model.
php artisan make:factory ProductFactory
After defining your database schema in migration files, you need to run them to create the tables.
To apply migrations, use this command:
php artisan migrate
Undo the most recent migration changes if something went wrong:
php artisan migrate:rollback
Rolling back and re-running all migrations is useful when resetting your database, especially with test data:
php artisan migrate:refresh --seed
Database seeders allow you to populate your database with initial data or test data.
Populate the database using all configured seeders:
php artisan db:seed
To run a specific seeder:
php artisan db:seed --class=SeederName
To run a seeder with migration:
php artisan migrate --seed
Working with routes is a core part of Laravel, and Artisan provides commands to list, clear, and cache routes.
To view all routes in your application:
php artisan route:list
If you’re updating routes, clear the cache to apply changes:
php artisan route:clear
To clear or cache various parts of your Laravel application, use these commands.
Clear the cached configuration files:
php artisan config:clear
If you encounter view display issues, clearing the view cache can help:
php artisan view:clear
Laravel Tinker is a REPL (Read-Eval-Print Loop) that lets you interact with your application directly.
Launch an interactive session to interact with your models and other classes:
php artisan tinker
For tasks like sending emails or processing data in the background, Laravel’s queue system can be managed with Artisan.
Run a worker to start processing queued jobs:
php artisan queue:work
If you have failed jobs, retry them all with this command:
php artisan queue:retry all
To view a list of failed jobs:
php artisan queue:failed
Laravel events and listeners help with decoupling and managing different parts of your application.
To create a new event:
php artisan make:event EventName
Example: Generate a UserRegistered
event.
php artisan make:event UserRegistered
Listeners listen to events and handle specific actions:
php artisan make:listener ListenerName
Example: Generate a SendWelcomeEmail
listener.
php artisan make:listener SendWelcomeEmail
Notifications are a great way to send alerts to users via email, SMS, or other channels:
php artisan make:notification NotificationName
If you’re working with Laravel Breeze or another package, you can scaffold your application with authentication:
php artisan breeze:install
Testing is crucial to ensure your application works as expected. Laravel provides a simple command to run your tests.
Run all the tests in your Laravel application:
php artisan test
Mastering these Laravel Artisan commands will make your development workflow faster, more efficient, and organized. Whether you’re setting up a project, managing database migrations, working with routes, or optimizing performance, Artisan is your command-line companion for it all.
Share and comment if we forgot any commands.
Have fun coding! Please let me know if you require any additional information regarding any of the commands.
Recent posts form our Blog
0 Comments
Like 1