Loading image

Blogs / Programming

Usefull Laravel Artisan Commands with Examples

Usefull Laravel Artisan Commands with Examples

  • showkat ali
  • 0 Comments
  • 35 View

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.

 

1. Setting Up Your Laravel Project

Starting a Laravel project from scratch? Here’s how to get your application up and running.

Create a New Laravel Project

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

Serve the Application Locally

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

 


2. Configuring and Optimizing Laravel

Before diving into development, it's helpful to clear caches and optimize configurations for faster response times.

Clear All Cached Data

When working with cached configurations, routes, and views, this command will clear all cache files in one go:

php artisan optimize:clear

Optimize Application Performance

To cache configuration and route files for faster performance, use:

php artisan optimize

 

php artisan make:model ModelName -mcr

 


3. Creating Models, Migrations, Controllers, and Factories

Artisan makes it simple to generate the files you need to interact with your database and control your app’s logic.

Create a Model with Migration, Controller, and Resource

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

 

Create a Factory

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

 


4. Running Migrations

After defining your database schema in migration files, you need to run them to create the tables.

Run All Pending Migrations

To apply migrations, use this command:

php artisan migrate

Rollback the Last Migration

Undo the most recent migration changes if something went wrong:

php artisan migrate:rollback

Refresh All Migrations with Seed Data

Rolling back and re-running all migrations is useful when resetting your database, especially with test data:

php artisan migrate:refresh --seed

 


5. Database Seeding

Database seeders allow you to populate your database with initial data or test data.

Run All Seeders

Populate the database using all configured seeders:

php artisan db:seed

Run a Specific Seeder

To run a specific seeder:

php artisan db:seed --class=SeederName

Run a Seeder with Migration

To run a seeder with migration:

php artisan migrate --seed

 


 

6. Managing Routes and Middleware

Working with routes is a core part of Laravel, and Artisan provides commands to list, clear, and cache routes.

List All Registered Routes

To view all routes in your application:

php artisan route:list

Clear Route Cache

If you’re updating routes, clear the cache to apply changes:

php artisan route:clear

 


7. Working with Caching and Views

To clear or cache various parts of your Laravel application, use these commands.

Clear Config Cache

Clear the cached configuration files:

php artisan config:clear

Clear Compiled Views

If you encounter view display issues, clearing the view cache can help:

php artisan view:clear

 


8. Interact with Your Application Using Tinker

Laravel Tinker is a REPL (Read-Eval-Print Loop) that lets you interact with your application directly.

Start Artisan Tinker

Launch an interactive session to interact with your models and other classes:

php artisan tinker

 


 

9. Managing Queues and Jobs

For tasks like sending emails or processing data in the background, Laravel’s queue system can be managed with Artisan.

Start the Queue Worker

Run a worker to start processing queued jobs:

php artisan queue:work

Retry All Failed Jobs

If you have failed jobs, retry them all with this command:

php artisan queue:retry all

View Failed Jobs

To view a list of failed jobs:

php artisan queue:failed

 


10. Events, Listeners, and Notifications

Laravel events and listeners help with decoupling and managing different parts of your application.

Generate an Event

To create a new event:

php artisan make:event EventName

Example: Generate a UserRegistered event.

php artisan make:event UserRegistered

Generate a Listener

Listeners listen to events and handle specific actions:

php artisan make:listener ListenerName

 

 

 

Example: Generate a SendWelcomeEmail listener.

php artisan make:listener SendWelcomeEmail

Generate a Notification

Notifications are a great way to send alerts to users via email, SMS, or other channels:

php artisan make:notification NotificationName

 


11. Authentication and Authorization

If you’re working with Laravel Breeze or another package, you can scaffold your application with authentication:

Install Authentication Scaffolding (Laravel Breeze)

php artisan breeze:install

 


12. Running Tests

Testing is crucial to ensure your application works as expected. Laravel provides a simple command to run your tests.

Run All Tests

Run all the tests in your Laravel application:

php artisan test

 


 

Conclusion

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.

 

 

 

  • Programming
showkat ali Author

showkat ali

Greetings, I'm a passionate full-stack developer and entrepreneur based in Pakistan. I specialize in PHP, Laravel, React.js, Node.js, JavaScript, and Python. I own interviewsolutionshub.com, where I share tech tutorials, tips, and interview questions. I'm a firm believer in hard work and consistency. Welcome to interviewsolutionshub.com, your source for tech insights and career guidance

0 Comments

Post Comment

Recent Blogs

Recent posts form our Blog

how to install node js in shared hosting server

how to install node js in shared hosting server

showkat ali
/

Read More
How to Use JavaScript Array Filter() With an Examples

How to Use JavaScript Array Filter() With an Examples

showkat ali
/
Programming

Read More
List of CDN Links for jQuery and Bootstrap: All Versions Included

List of CDN Links for jQuery and Bootstrap: All Versions Included

showkat ali
/
Programming

Read More
How to use DataTables in Laravel 11

How to use DataTables in Laravel 11

showkat ali
/
Programming

Read More
Timeline Chart bar with date axis to be displayed on top

Timeline Chart bar with date axis to be displayed on top

showkat ali
/
Programming

Read More
Spatie's Role and Permission package in Laravel

Spatie's Role and Permission package in Laravel

Muhammad Abbas
/
Programming

Read More