Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

MdMostaFizurRahaman/ecommerce

29 stars
1 code files
View MdMostaFizurRahaman/ecommerce on GitHub

app/Console/Commands/GenerateSuperAdmin.php

Open in GitHub
use App\Models\Role;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
 
class GenerateSuperAdmin extends Command
{
protected $signature = 'generate:super-admin';
 
protected $description = 'This is create a super admin for the system.';
 
public function __construct()
{
parent::__construct();
}
 
public function handle()
{
 
// Get the inputs
$name = $this->ask('Please input user name');
$email = $this->ask('Please input email');
$password = $this->secret('Please input password');
$confirm = $this->secret('Please confirm password');
 
// Validate the inputs
$validator = Validator::make([
'name' => $name,
'email' => $email,
'password' => $password,
'password_confirmation' => $confirm,
], [
'name' => ['required', 'max:50'],
'email' => ['required', 'email', 'unique:users,email'],
'password' => ['required', 'min:4', 'confirmed'],
]);
 
// Return validation errors
if ($validator->fails()) {
$this->info('Super admin is not created. See error messages below:');
 
foreach ($validator->errors()->all() as $error) {
$this->error($error);
}
return 1;
}
 
// Create super admin
DB::transaction(function () use ($name, $email, $password) {
$admin = User::create([
'name' => $name,
'email' => $email,
'password' => bcrypt($password),
]);
});
 
$this->info('Super Admin created successfully.');
 
return 0;
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.