How to create custom command in Laravel 8

How to create custom command using Laravel 8

Posted by Raquel Crist on May 14, 2021

How to create custom command in Laravel 8

How to create custom command using Laravel 8

Posted by Raquel Crist on May 14, 2021

In this tutorial you will learn about how to create custom command in Laravel using Laravel 8.

Make sure to set up the project with an active database connection and migrated the default files.

Step 1. Create Laravel command by executing command:

php artisan make:command CreateUsersCommand

Go to "app/Console/Commands" and edit the file CreateUsersCommand.php:

namespace App\Console\Commands;

use App\Models\User;

use Illuminate\Console\Command;

use Illuminate\Support\Facades\Hash;

class CreateUsersCommand extends Command {

/**

* The name and signature of the console command.

*

* @var string

*/

protected $signature = 'add:user';



* The console command description.

*

* @var string

*/

protected $description = 'Add users to database';

/**

* Create a new command instance.

*

* @return void

*/

public function __construct() {

parent::__construct();

}

/**

* Execute the console command.

*

* @return int

*/

public function handle() {

    $input['name'] = $this->ask('Your name?');

    $input['email'] = $this->ask('Your email?');

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

if($input['name'] != null && $input['email'] != null && $input['password'] != null) {

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

    User::create($input);

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

    return 1;

}else{

    $this->info('Create user failed. All details are required');

    return 0;

   }

  }

}

Step 2. Register command to console Kernel

Go to "app/Console" and edit the file 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\CreateUsersCommand::class,

];

/**

* Define the application's command schedule.

*

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

* @return void

*/

protected function schedule(Schedule $schedule) {

   // $schedule->command('inspire')->hourly();

}

/**

* Register the commands for the application.

*

* @return void

*/

protected function commands() {

   $this->load(__DIR__.'/Commands');

   require base_path('routes/console.php');

}

}

Step 3. Check if command is registered by running command:

php artisan list

Step 4. Now that you can see the command. Use the command to create a user

php artisan add:user

Now If you check the database you should see the user that was created. 

If you wish to download this tutorial go to https://github.com/wecode101/custom_command_laravel_8

This field is required
Your question have been successfully submitted and will be reviewed before published
Please login to ask a question