-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create interactive artisan command to add user with role
- Loading branch information
Showing
3 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use App\Models\User; | ||
use App\Models\Role; | ||
|
||
class AddUser extends Command | ||
{ | ||
protected $signature = 'user:add'; | ||
|
||
protected $description = 'Add a new user with a role'; | ||
|
||
public function handle() | ||
{ | ||
$firstName = $this->ask('Enter the user\'s first name'); | ||
$lastName = $this->ask('Enter the user\'s last name'); | ||
$email = $this->ask('Enter the user\'s email'); | ||
|
||
// Fetch available roles | ||
$roles = Role::all()->pluck('label')->toArray(); | ||
$role = $this->choice('Select a role for the user', $roles); | ||
|
||
// Create the user | ||
$user = User::create([ | ||
'first' => $firstName, | ||
'last' => $lastName, | ||
'fullname' => "{$firstName} {$lastName}", | ||
'email' => $email, | ||
'username' => $email, | ||
]); | ||
|
||
// Assign the role to the user | ||
$user->assignRole($role); // Ensure you have a method to assign roles | ||
|
||
$this->info("User {$firstName} {$lastName} with role {$role} has been created successfully."); | ||
} | ||
} |
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
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