Laravel Create Custom Artisan Command with Example

Laravel Create Custom Artisan Command with Example

In this post we will give you information about Laravel Create Custom Artisan Command with Example. Hear we will give you detail about Laravel Create Custom Artisan Command with ExampleAnd how to use it also give you demo for it if it is necessary.

It’s better if we make our own artisan command for like project setup, create new admin user etc in our laravel application. If we create custom command then we can make project setup like create one admin user, run migration, run seeder and etc. In this post i give you example, how to create console commands in laravel project.

In this example will create custom command for create new admin user. This custom command will ask your name, email and password. In this example we will create command like:

Custom command

php artisan admins:create

This command through we will create new admins. So first fire bellow command and create console class file.

php artisan make:console AdminCommand --command=admins:create

After this command you can find one file AdminCommand class in console directory. so one that file and put bellow code.

app/Console/Commands/AdminCommand.php

namespace AppConsoleCommands;


use IlluminateConsoleCommand;

use Hash;

use DB;


class AdminCommand extends Command

{

/**

* The name and signature of the console command.

*

* @var string

*/

protected $signature = 'admins:create';


/**

* The console command description.

*

* @var string

*/

protected $description = 'Command description';


/**

* Create a new command instance.

*

* @return void

*/

public function __construct()

{

parent::__construct();

}


/**

* Execute the console command.

*

* @return mixed

*/

public function handle()

{

$input['name'] = $this->ask('What is your name?');

$input['email'] = $this->ask('What is your email?');

$input['password'] = $this->secret('What is the password?');

$input['password'] = Hash::make($input['password']);


DB::table('admins')->insert($input);

$this->info('Admin Create Successfully.');

}

}


Ok, now we need to register this command class in Kernel.php file, so open file and add class this way:

app/Console/Kernel.php

namespace AppConsole;


use IlluminateConsoleSchedulingSchedule;

use IlluminateFoundationConsoleKernel as ConsoleKernel;


class Kernel extends ConsoleKernel

{

/**

* The Artisan commands provided by your application.

*

* @var array

*/

protected $commands = [

CommandsAdminCommand::class,

];

/**

* Define the application's command schedule.

*

* @param IlluminateConsoleSchedulingSchedule $schedule

* @return void

*/

protected function schedule(Schedule $schedule)

{


}

}

Now we are ready to use or custom command, you fire bellow command and check you can find command in the list this way “admins:create”.

php artisan list

php artisan admins:create

you can checkkk….

Hope this code and post will helped you for implement Laravel Create Custom Artisan Command with Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

6  +    =  10

We accept paid advance and paid guest Posting on our Site : Contact US