在 Laravel 手动注册用户

是否有可能手动注册一个用户(与工匠?) ,而不是通过认证注册页?

我只需要少量的用户帐户,并且想知道是否有一种方法可以创建这些帐户,而不必设置注册控制器和视图。

131499 次浏览

Yes, you can easily write a database seeder and seed your users that way.

Yes, the best option is to create a seeder, so you can always reuse it.

For example, this is my UserTableSeeder:

class UserTableSeeder extends Seeder {


public function run() {


if(env('APP_ENV') != 'production')
{
$password = Hash::make('secret');


for ($i = 1; $i <= 10; $i++)
{
$users[] = [
'email' => 'user'. $i .'@myapp.com',
'password' => $password
];
}


User::insert($users);
}
}

After you create this seeder, you must run composer dumpautoload, and then in your database/seeds/DatabaseSeeder.php add the following:

class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();


$this->call('UserTableSeeder');
}
}

Now you can finally use php artisan db:seed --class=UserTableSeeder every time you need to insert users in the table.

You can use Model Factories to generate a couple of user account to work it. Writing a seeder will also get the job done.

I think you want to do this once-off, so there is no need for something fancy like creating an Artisan command etc. I would suggest to simply use php artisan tinker (great tool!) and add the following commands per user:

$user = new App\Models\User();
$user->password = Hash::make('the-password-of-choice');
$user->email = 'the-email@example.com';
$user->name = 'My Name';
$user->save();

This is an old post, but if anyone wants to do it with command line, in Laravel 5.*, this is an easy way:

php artisan tinker

then type (replace with your data):

DB::table('users')->insert(['name'=>'MyUsername','email'=>'thisis@myemail.com','password'=>Hash::make('123456')])

You can also create a new console command which can be called from the command line. This is especially useful if you want to create new users on demand.

This example makes use of laravel fortify but you can also use your own user registration logic.

First create a new console command:

php artisan make:command CreateUserCommand

Then add the implementation:

<?php


namespace App\Console\Commands;


use Illuminate\Console\Command;
use App\Actions\Fortify\CreateNewUser;


class CreateUserCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:create {--u|username= : Username of the newly created user.} {--e|email= : E-Mail of the newly created user.}';


/**
* The console command description.
*
* @var string
*/
protected $description = 'Manually creates a new laravel user.';


/**
* Execute the console command.
* https://laravel.com/docs/9.x/artisan
*
* @return int
*/
public function handle()
{
// Enter username, if not present via command line option
$name = $this->option('username');
if ($name === null) {
$name = $this->ask('Please enter your username.');
}


// Enter email, if not present via command line option
$email = $this->option('email');
if ($email === null) {
$email = $this->ask('Please enter your E-Mail.');
}


// Always enter password from userinput for more security.
$password = $this->secret('Please enter a new password.');
$password_confirmation = $this->secret('Please confirm the password');


// Prepare input for the fortify user creation action
$input = [
'name' => $name,
'email' => $email,
'password' => $password,
'password_confirmation' => $password_confirmation
];


try {
// Use fortify to create a new user.
$new_user_action = new CreateNewUser();
$user = $new_user_action->create($input);
}
catch (\Exception $e) {
$this->error($e->getMessage());
return;
}


// Success message
$this->info('User created successfully!');
$this->info('New user id: ' . $user->id);
}
}

You can execute the command via:

php artisan user:create -u myusername -e mail@example.com

I recommend to always ask for the password via the user input, not as parameter for security reasons.