Last active
August 6, 2021 13:03
-
-
Save rutcreate/a7bb9db09b15724c5261ce9ce3699cb2 to your computer and use it in GitHub Desktop.
Laravel User Console Commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use App\Models\User; | |
use Illuminate\Console\Command; | |
class UserBlockCommand extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'user:block'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Block user'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$users = User::orderBy('email', 'asc')->pluck('email'); | |
$email = $this->anticipate('Email address', $users); | |
if (!$this->confirm('Would you like to block ' . $email . '?')) { | |
$this->error('Abort'); | |
return; | |
} | |
$user = User::where('email', $email)->first(); | |
$user->is_active = false; | |
if ($user->save()) { | |
$this->info('User has been blocked.'); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use App\Models\User; | |
class UserChangePasswordCommand extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'user:change-password'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Change Password'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$users = User::orderBy('email', 'asc')->pluck('email'); | |
$email = $this->anticipate('Email address', $users); | |
$password = $this->secret('Password'); | |
$confirmPassword = $this->secret('Password again'); | |
if ($password != $confirmPassword) { | |
$this->error('Password is not same'); | |
return; | |
} | |
$user = User::where('email', $email)->first(); | |
$user->password = bcrypt($password); | |
if ($user->save()) { | |
$this->info('Password has been changed'); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use App\Models\User; | |
class UserCreateCommand extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'user:create'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Create user'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$name = $this->ask('Display name'); | |
$email = $this->ask('Email address'); | |
$password = $this->secret('Password'); | |
$confirmPassword = $this->secret('Password again'); | |
if ($password != $confirmPassword) { | |
$this->error('Password is not same'); | |
return; | |
} | |
$is_admin = $this->confirm('Is Admin?'); | |
$this->question("Display name: {$name}\nEmail address: {$email}\n"); | |
if ($this->confirm('Are you sure you want to create user with above information')) { | |
$user = new User; | |
$user->name = $name; | |
$user->email = $email; | |
$user->is_admin = $is_admin; | |
$user->is_active = true; | |
if (isset($password)) { | |
$user->password = bcrypt($password); | |
} | |
if ($user->save()) { | |
$this->info('User has been created.'); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment