Creating first user in Laravel

Laravel has some great functionality to seed databases, but they are designed primarily to be used in development and testing environments. That is not to say that they cannot be leveraged for use in production.

There is, however, an easy and quick way to create models in the production environment to get you started.

Scenario

If you have an application you are about to deploy which does not allow user registration but requires an authenticated user for admin tasks, you may find yourself wondering how to get that initial user in the database. You could use a management tool such as phpmyadmin or pgadmin, but Laravel has an easy way to create a user which a few short commands.

Tinker

Tinker is a REPL which is included in every Laravel app. To access it’s command line just enter the following command.

user@host:~# php artisan tinker

Then to add your initial user,

Psy Shell v0.9.9 (PHP 7.3.9-1~deb10u1 — cli) by Justin Hileman
         
>>> User::create([
   'name' => 'simon',
   'email' => 'username@example.com',
   'password' => Hash::make('my_strong_password'),
]);
[!] Aliasing 'User' to 'App\User' for this Tinker session.
 => App\User {#3278
      name: "simon",
      email: "username@example.com",
      updated_at: "2019-09-26 10:34:45",
      created_at: "2019-09-26 10:34:45",
      id: 1,
    }         

You will now be able to login through the web interface of your app using the email and password you specified.