Manually register a user in Laravel
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 = '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')])
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.