php 在 Laravel 中手动注册用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35753951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:34:12  来源:igfitidea点击:

Manually register a user in Laravel

phplaravellaravel-5laravel-5.2

提问by Matt Ellis

Is it possible to manually register a user (with artisan?) rather than via the auth registration page?

是否可以手动注册用户(使用工匠?)而不是通过身份验证注册页面?

I only need a handful of user accounts and wondered if there's a way to create these without having to set up the registration controllers and views.

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

回答by Christoffer Tyrefors

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:

我认为您想一次性完成此操作,因此不需要创建 Artisan 命令等花哨的东西。我建议您简单地使用php artisan tinker(很棒的工具!)并为每个用户添加以下命令:

$user = new App\User();
$user->password = Hash::make('the-password-of-choice');
$user->email = '[email protected]';
$user->name = 'My Name';
$user->save();

回答by Diverti

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

这是一篇旧帖子,但如果有人想在 Laravel 5.* 中使用命令行来完成,这是一种简单的方法:

php artisan tinker

then type (replace with your data):

然后键入(替换为您的数据):

DB::table('users')->insert(['name'=>'MyUsername','email'=>'[email protected]','password'=>Hash::make('123456')])

回答by user2094178

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

是的,最好的选择是创建一个播种机,这样您就可以随时重复使用它。

For example, this is my UserTableSeeder:

例如,这是我的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.phpadd the following:

创建此播种机后,您必须运行composer dumpautoload,然后在您的database/seeds/DatabaseSeeder.php添加以下内容:

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=UserTableSeederevery time you need to insert users in the table.

现在你终于可以在php artisan db:seed --class=UserTableSeeder每次需要在表中插入用户时使用了。

回答by Francesco de Guytenaere

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

是的,您可以轻松地编写数据库播种器并以这种方式播种您的用户。

回答by oseintow

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

您可以使用Model Factories生成几个用户帐户来工作。编写播种机也将完成工作。