Laravel 8 create custom artisan command example
As we know, Laravel provides artisan commands to interact with application. You really wonder how the artisan command works. You can also create your custom artisan command.
In this article, we will discuss on what is Artisan, how artisan command works. We will also take an example of custom artisan command which will generate random data.
What is the Artisan
Artisan is a command line tool in Laravel to interact with the application. Artisan helps in lot way to create application classes, databases, migrations, run server or view application list.
You can get list of all Artisan command using following command:
php artisan list
How artisan command works
Laravel artisan command works two ways into application. Through inbuilt commands and custom commands. To know how the artisan command works, view artisan file from the root of the application. The artisan file loads application composer classes. bootstrap/app.php
will create a instance of the application and return app.
When we run any artisan command, the CLI command will be executed in this console and the response will sent back to a terminal or another output device for the developers. Then $kernel's terminate() method will shut down events before application cycle ends.
Create custom artisan command
You can also create your custom command which will execute blocks of code as per requirements. This includes sending emails, generating default database, run cronjob etc.
Suppose after setting up a application, we want admin user to login into application. Then first we can first create a command which will create a default admin user. So everytime we setup application, we run command and create admin user.
First run following artisan command:
php artisan make:command CreateAdminUser
This will create a command class at app/Console/Commands/CreateAdminUser.php
file. Open the file.
$signature
will be the command that you will execute into Terminal. In our case set $signature
to admin:generate
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'admin:generate';
So our command will be:
php artisan admin:generate
You can set $description
as you may wish. This will tell you what the command does.
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create admin user';
Now come to our handle()
method which is the code block function executed when command run. Add the code into handle()
method as below:
/**
* Execute the console command.
*
* @return App\Models\User
*/
public function handle()
{
$user = [
'name' => 'Jitesh Meniya',
'email' => '[email protected]',
'password' => Hash::make('123456')
];
App\Models\User::create($user);
}
Below is the final CreateAdminUser
class file.
<?php
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
class CreateAdminUser extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'admin:generate';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create admin user';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return App\Models\User
*/
public function handle()
{
$user = [
'name' => 'Jitesh Meniya',
'email' => '[email protected]',
'password' => Hash::make('123456')
];
User::create($user);
}
}
Now command is ready. Set database credentials into .env file as per your MySQL.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=commands
DB_USERNAME=root
DB_PASSWORD=secret
And run the following command into Terminal
php artisan admin:generate
This will generate new admin into users table.
Passing argument with command
Now suppose we want to pass admin email and password value with command instead of generating static email and password.
To pass the argument with command first, change $signature property as below:
protected $signature = 'admin:generate {email} {password}';
Or you can also set default value if the argument is not passed with command.
protected $signature = 'admin:generate {[email protected]} {password=123456}';
And use $this->argument() method to retrieve value into handle() method.
$email = $this->argument('email');
$password = $this->argument('password');
Below is the final CreateAdminUser class preview:
<?php
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
class CreateAdminUser extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'admin:generate {[email protected]} {password=123456}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create admin user';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return App\Models\User
*/
public function handle()
{
$email = $this->argument('email');
$password = $this->argument('password');
$user = [
'name' => 'Jitesh Meniya',
'email' => $email,
'password' => Hash::make($password)
];
User::create($user);
}
}
Now run the command with email and password argument. If no argument will pass, the admin will be created with default email and password.
php artisan admin:generate [email protected] mypasword
Conclusion
This way, you can use create your own command and execute query from artisan command. You can do any task like cronjob, queue work etc. I hope you liked this article and will help in your web development.
Thanks for reading the article.
Copyright 2023 HackTheStuff