Create a custom artisan command example; Through this tutorial, we will learn how to create and use a custom artisan command in laravel 9 apps.
Laravel 9 Create Custom Artisan Command Example
Use the following steps to create and use custom artisan command in laravel 9 apps:
- Step 1: Download New Laravel 9 App
- Step 2: Connect Laravel App to Database
- Step 3: Create Artisan Command
- Step 4: Update Code in Artisan Command
- Step 5: Run Artisan Command
Laravel is a popular PHP framework that provides various features to build web applications quickly and easily. One of its powerful features is Artisan, a command-line interface that allows developers to perform various tasks such as generating boilerplate code, running database migrations, and more. In this tutorial, we will learn how to create a custom Artisan command in Laravel 9.
Step 1: Download New Laravel 9 App
The first step is to download a new Laravel 9 app. You can do this by opening your terminal or command prompt and entering the following command:
composer create-project --prefer-dist laravel/laravel myapp
This command will download a new Laravel 9 app in a directory named “myapp”. Once the download is complete, navigate to the app directory by entering the following command:Copy
cd myapp
Step 2: Connect Laravel App to Database
Before we can create a custom Artisan command, we need to connect our Laravel app to a database. Open the “.env” file in the root directory of your app and update the following fields with your database credentials:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database_name DB_USERNAME=your_database_username DB_PASSWORD=your_database_password
Save the file and exit.
Step 3: Create Artisan Command
Now that our Laravel app is connected to the database, we can create a custom Artisan command. To create a new Artisan command, enter the following command in your terminal:Copy
php artisan make:command CustomCommand
This command will generate a new file named “CustomCommand.php” in the “app/Console/Commands” directory.
Step 4: Update Code in Artisan Command
Open the “CustomCommand.php” file and update the following fields:
$signature
: This field specifies the name of your custom Artisan command. For example, if you want to name your command “greet”, you can update the$signature
field as follows:
protected $signature = 'greet';
$description
: This field specifies the description of your custom Artisan command. You can update it to describe what your command does. For example:
protected $description = 'This command greets the user.';
handle()
: This is the main method of your custom Artisan command. It contains the logic that your command will execute. Update the method to perform the task you want your command to do. For example:
public function handle() { $this->info('Hello, welcome to my custom Artisan command!'); }
Step 5: Run Artisan Command
Now that we have defined our custom Artisan command, we can run it by entering the following command in our terminal:Copy
php artisan greet
This will execute our custom Artisan command and output the message specified in the handle()
method.
Conclusion
In this tutorial, we learned how to create a custom Artisan command in Laravel 9. We started by downloading a new Laravel 9 app, connecting it to the database, and creating a new Artisan command. We then updated the code in our Artisan command to perform the task we wanted it to do and ran the command in our terminal. I hope you found this tutorial helpful and that it inspires you to create your own custom Artisan commands in Laravel 9.