How to Create Users on a Laravel Vapor Site

How do you create a user on a brand new Vapor deployment? I'll show you.

One of the problems I needed to overcome when making this blog was to create a default administrator user that can access the Laravel Nova dashboard and start managing other users and pieces of content. On a typical server deployment, I would simply get my website deployed and then CLI into the server and Tinker my way to a new user. In Laravel Vapor's serverless deployment, that option is not available - but an even simpler way is!

I have a lot of seeders and factories that I use for testing, and I didn't want a lot of crud in the production database. I didn't want to run all my seeders - I really only needed on user to get me going.

So I created a process where I created a User seeder with my credentials, deployed it to production, and then from my local machine I ran a Vapor command line to trigger that one seeder. This worked like a charm.

  1. Create a table seeder
php artisan make:seed UserTableSeeder

I like to have mine collect a list of users in case I want to create more than one.

<?php

use Illuminate\Database\Seeder;

class UserTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        collect([
            [
                'name'     => 'Your Name',
                'email'    => 'email@name.com',
                'password' => bcrypt('SOMEPASSWORD'),
            ],
        ])->each(function ($item) {
            $user = \App\User::create($item);
        });    
    }
}
  1. Deploy to changes to Vapor production
vapor deploy production
  1. Run the seeder
vapor command production --command="db:seed --class='UserTableSeeder' --force"

This works with staging too, just replace production values with staging

Braden Keith