How can make Custom Command On laravel

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

hello dev,

today we learn How can make Custom Command On laravel flow all process which and steps:

Step:1

In your Laravel application, you may want to create your own craftsmanship commands for setting up your project, or create a new admin user. You can set up your project by creating a custom command. Examples: Creating an admin user, running a migration, running a seeder, etc. This post shows an lesson of how to create a console command in your Laravel project.

In this situation will create custom command for create new admin consumerthis practice command will ask your calle-mail and password. In this lesson 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

Step:2

After this cmd on  file AdminCommand class in console folder. so one that file and put bellow code.

app/Console/Commands/AdminCommand.php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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.');

}

}

Step:3

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

app/Console/Kernel.php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;

use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel

{

/**

* The Artisan commands provided by your application.

*

* @var array

*/

protected $commands = [

Commands\AdminCommand::class,

];

/**

* Define the application's command schedule.

*

* @param \Illuminate\Console\Scheduling\Schedule $schedule

* @return void

*/

protected function schedule(Schedule $schedule)

{

}

}

Step:4

Now we are run  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

Lets make a fun.

thank you for your time for flow all steps.

Recent Posts

Related Posts