[翻译]  Manually register a user in Laravel

[CHINESE]  在Laravel中手动注册用户


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.

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

4 个解决方案

#1


39  

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 = 'the-email@example.com';
$user->save();

#2


4  

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

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

#3


2  

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.php add the following:

创建此播种器后,必须运行composer dumpautoload,然后在数据库/ 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=UserTableSeeder every time you need to insert users in the table.

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

#4


1  

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

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


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号