DEV Community

Cover image for Essential Laravel Commands Every Developer Should Know
Richa
Richa

Posted on

Essential Laravel Commands Every Developer Should Know

Laravel, a strong PHP framework, makes web development both easy and exciting. Composer and Artisan are two popular Laravel development tools. These tools improve the speed of development by automating operations managing dependencies, and ensuring a clean workflow. In this article, we'll look at the most helpful commands and their functionality, to help you improve your Laravel projects.

πŸ” What is Composer?

Composer acts as a "helper" in organizing the tools and libraries required for your project. Imagine you're building a house and require particular resources such as bricks, cement, and equipment. Instead than visiting multiple stores to find each item, Composer automatically gathers them all in one location.
In the context of Laravel:

  • Composer enables the installation of Laravel and additional packages (such as libraries) required for your project to run properly.
  • It also updates these packages if a new version becomes available.

πŸ“Œ For example, if you want to create a new Laravel project, simply run this command:

composer create-project laravel/laravel laravel-app
Enter fullscreen mode Exit fullscreen mode

And Composer will take care of setting everything up for you!

πŸ” What is Artisan?

Artisan is similar to a magic toolkit for Laravel. It has a number of built-in commands (think of them as tools) that allow you to perform common operations in your Laravel project without having to do them manually.
πŸ“Œ For example

  • It can generate files for you (such as models, controllers, and migrations).
  • It is capable of running tasks such as database migration and data seeding.
  • It allows you to serve your app on a local server while development.

With Artisan, you don't have to create everything from scratch; simply run the suitable commands, and it will handle the rest!
πŸ“Œ For example, to create a new controller, you can run:

php artisan make:controller MyController
Enter fullscreen mode Exit fullscreen mode

This will automatically generate the necessary code for you!

In short:

  • Composer is used to manage the tools and packages that your Laravel project requires.
  • Artisan is a command-line tool that makes it easier to do tasks within Laravel.

πŸ’» Laravel Commands

Command Description Usage Example
composer create-project laravel/laravel Creates a new Laravel project. composer create-project laravel/laravel laravel-app
composer install Installs all the dependencies listed in the composer.json file. composer install
composer update Updates all the dependencies to their latest versions based on the composer.json file. composer update
composer require <package-name> Installs a new package and adds it to the composer.json file. composer require monolog/monolog
composer remove <package-name> Removes a package from the project and updates the composer.json file. composer remove monolog/monolog
composer dump-autoload Regenerates the autoload files, especially useful if you add/remove classes manually. composer dump-autoload
composer show Displays information about installed packages and their versions. composer show
composer show <package-name> Displays detailed information about a specific package. composer show monolog/monolog
composer outdated Shows the list of outdated packages in the project. composer outdated
composer install --prefer-dist Installs the dependencies from the distribution archive rather than cloning the Git repository. composer install --prefer-dist
composer self-update Updates Composer to the latest stable version. composer self-update
php artisan about Displays information about your Laravel application. php artisan about
php artisan make:migration Creates a new migration file for database schema changes. php artisan make:migration create_user_table
php artisan migrate:refresh Rolls back all migrations and re-runs them. php artisan migrate:refresh
php artisan migrate:rollback Rolls back the last batch of migrations. php artisan migrate:rollback
php artisan migrate:reset Rolls back all database migrations. php artisan migrate:reset
php artisan migrate:status Shows the status of each migration (run or pending). php artisan migrate:status
php artisan make:seeder Creates a seeder file for inserting sample data into the database. php artisan make:seeder UserTableSeeder
php artisan db:seed Runs the specified seeder. php artisan db:seed --class=UserTableSeeder
php artisan list Displays all available Artisan commands. php artisan list
php artisan help Shows help for a specific Artisan command. php artisan help make:model
php artisan make:model Generates a new Eloquent model. php artisan make:model User
php artisan make:controller Creates a new controller file. php artisan make:controller UserController
php artisan make:request Creates a new form request class for validation logic. php artisan make:request UserRequest
php artisan make:resource Creates a resource class for formatting API responses. php artisan make:resource UserResource
php artisan route:list Displays a list of all registered routes in the application. php artisan route:list
php artisan route:cache Caches the application's routes for faster performance. php artisan route:cache
php artisan config:cache Caches the application configuration for improved performance. php artisan config:cache
php artisan cache:clear Clears the application cache. php artisan cache:clear
php artisan view:clear Clears compiled views to resolve rendering issues. php artisan view:clear
php artisan config:clear Clears the configuration cache. php artisan config:clear
php artisan optimize Optimizes the application by combining various cache commands. php artisan optimize
php artisan queue:work Processes jobs from the queue. php artisan queue:work
php artisan queue:restart Restarts the queue worker after code changes. php artisan queue:restart
php artisan schedule:run Runs the scheduled tasks defined in the Kernel.php. php artisan schedule:run
php artisan make:event Creates a new event class. php artisan make:event UserRegistered
php artisan make:listener Creates a listener class for handling events. php artisan make:listener SendWelcomeEmail
php artisan tinker Opens an interactive shell (REPL) to test or debug code. php artisan tinker
php artisan serve Starts the local development server. php artisan serve
php artisan down Puts the application into maintenance mode. php artisan down --message="Site under maintenance"
php artisan up Brings the application out of maintenance mode. php artisan up

Conclusion

Composer and Artisan are essential tools for making Laravel development faster, easier, and more efficient. While Composer makes dependency management easier, Artisan provides a number of options for automating regular workflows. Mastering these commands will allow you to save time and focus on developing high-quality applications.

Share your thoughts!

Which Laravel commands do you use frequently? Share in the comments section below! Let's learn together and improve Laravel development.

Happy Coding!✨

Top comments (0)