Laravel Commands
laravel artisan

Laravel Commands

Laravel is a popular PHP framework that provides a set of commands, known as Artisan commands, to help you manage and develop Laravel applications more efficiently. These commands are available through the command-line interface (CLI) and provide a range of functionalities, such as generating code, managing migrations, running tests, and performing various other tasks.

Laravel Commands 🚀

🔧 Default Artisan Commands in Laravel

Laravel comes with a set of default commands, including:

  • php artisan make: for creating new skeleton classes like models and controllers.
  • php artisan create_table-name_table for generating migrations for the database.
  • php artisan db:seed for seeding the database using seeders and factories.

✨ Custom Commands

Creating a new command in Laravel is as easy as using the existing default commands. Follow these steps:

1️⃣ Use the command php artisan make:command <CommandName> to create a new command class.

2️⃣ Once the command class is created, running php artisan list will display the new command's signature. Now, the command is ready to use!

No alt text provided for this image
a command skeleton

Initially, the newly created command won't perform any action as the handle method is empty. You need to fill it with the desired functionality.

💡 Example

To demonstrate the usage of commands, let's create a command that utilizes the factories we created in the previous article on Laravel factories. This custom command will seed the database with vendors having businesses and categories.

To simplify the process, follow these steps:

  • 1️⃣ Create a new command using php artisan make:command SeedVendors.
  • 2️⃣ The command's signature should be seed:vendors.
  • 3️⃣ Specify the number of vendors you want (e.g., --count=3, default is 1).
No alt text provided for this image
SeedVendors command
  • 4️⃣ Optionally, include arguments like --businesses to determine the number of businesses for each vendor (default is 0) and --categories to set the number of categories for each business (default is 0).
  • 5️⃣ Add descriptions for each argument within the signature using curly braces ({}) and separating the argument from the description with a colon (:).

These descriptions can be viewed by running php artisan help seed:vendors.

No alt text provided for this image
seed:vendors command description

Now, every time you run php artisan seed:vendors 40 --businesses=4 --categories=3, you will generate 40 vendors, each with 4 businesses, and each business having 3 different categories in the database.

📚 For a more comprehensive understanding of Laravel commands, refer to the official Laravel documentation at https://laravel.com/docs/10.x/artisan.

Thank you for reading this far! You can find the code sample on my GitHub repository. Goodbye! 👋

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics