Artisan commands in Laravel 10 offer a powerful approach to automate activities and enhance the capabilities of your application’s command-line interface. While Laravel includes a number of built-in Artisan commands, there may be times when you need to write your own custom commands to do tasks particular to your application.
In this detailed article, we will walk you through the process of developing custom Artisan commands in Laravel 10. By creating your own commands, you may encapsulate complex operations, automate repetitive chores, and fully utilise Laravel’s command-line interface.
Read More: How To Generate Unique Slug in Laravel 10 Tutorial
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Laravel Artisan Command Panel
Open project into terminal and type this artisan command to see all available commands.
$ php artisan
This command will list all available commands in artisan panel.
Inside this list we should see a command called make:command. Have a look into this image.
make:command is used to create artisan command.
When we create custom artisan command, then command files will be stored inside /app/Console/Commands folder.
Initially, Commands folder will not be inside folder structure. But when we create command files, it will create that.
Read More: How To Get HTTP Hostname In Laravel 10 Tutorial
Command #1 – Create Basic Artisan Command
We will create a simple basic command which asks for user information and simply displays all information into console.
Run this command to create command file.
$ php artisan make:command UserInfo
Command file UserInfo.php will be created inside /app/Console/Commands folder.
Open UserInfo.php and write this code into it.
<?php namespace App\Console\Commands; use Illuminate\Console\Command; class UserInfo extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'user:info'; /** * The console command description. * * @var string */ protected $description = 'This is the command asks for user information and display'; /** * Execute the console command. * * @return int */ public function handle() { $name = $this->ask('What is your name?'); $email = $this->ask('What is your email address?'); $this->info('User Informations: Name - ' . $name . " and Email - " . $email); } }
- $signature – It gives command syntax to artisan panel.
- $description – Description of command
- $this->info – It provides a information line at console
You will see your created custom command into artisan command’s list.
Usage of Command
Back to terminal and run this command
$ php artisan user:info
Read More: How to Check If a File Exists in Laravel 10 Tutorial
Command #2 – Use of Model into Command File
Suppose, you have users table in your database and also User.php as a model file for that.
Here,
We create a command which asks for user information, we fill the user info into terminal and then it will save to database table.
Custom Command For Create User
Back to terminal and run this spark command.
$ php artisan make:command UserCreate
It will create a file UserCreate.php into /app/Console/Commands folder.
Open UserCreate.php and write this command.
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Support\Facades\Hash; use App\Models\User; class UserCreate extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'user:create'; /** * The console command description. * * @var string */ protected $description = 'This is the command asks for user information and create into database'; /** * Execute the console command. * * @return int */ public function handle() { $input['name'] = $this->ask('What is your name?'); $input['email'] = $this->ask('What is your email address?'); $input['password'] = $this->ask('Provide your secret password?'); $input['password'] = Hash::make($input['password']); User::create($input); $this->info("User created successfully"); } }
Usage of Command
Back to terminal and run this command
$ php artisan user:create
This command will asks for user information like name, email & password and then it will create user after that.
We hope this article helped you to learn about How To Create Custom Artisan Command in Laravel 10 in a very detailed way.
Read More: Laravel 10 How To Change Name And From Address in Email
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.