Manually register a user in Laravel

PhpLaravelLaravel 5Laravel 5.2

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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\User();
$user->password = Hash::make('the-password-of-choice');
$user->email = '[email protected]';
$user->name = 'My Name';
$user->save();

Solution 2 - Php

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'=>'[email protected]','password'=>Hash::make('123456')])

Solution 3 - Php

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.

Solution 4 - Php

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

Solution 5 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt EllisView Question on Stackoverflow
Solution 1 - PhpChristoffer TyreforsView Answer on Stackoverflow
Solution 2 - PhpDivertiView Answer on Stackoverflow
Solution 3 - Phpuser2094178View Answer on Stackoverflow
Solution 4 - PhpFrancesco de GuytenaereView Answer on Stackoverflow
Solution 5 - PhposeintowView Answer on Stackoverflow