Search code examples
laraveldatabase-migrationlaravel-seeding

Why doesn't this seeder class code work in production?


I have this seeder class that works/runs in my development setup:

class OwnerSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        if(config('owner.owner_first_name')) {
            $user = User::firstOrCreate(
                 ['email' => config('owner.owner_email')], [
                     'first_name' => config('owner.owner_first_name'),
                     'last_name' => config('owner.owner_last_name'),
                     'mobile_phone_number' => config('owner.owner_mobile_phone_number'),
                     'password' => bcrypt(config('owner.owner_password')),
                     'email_verified_at' => now(),
                     'identity_verified_at' => now(),
                 ]
             );
         }
        
         $user->assignRole('owner')
         ->givePermissionTo(Permission::all());
    }
}

But when I run it in production, I get the error 'Undefined variable $user'. What can I do to resolve this?


Solution

  • What I can see there is that your config is different in production and your local: the owner.owner_first_name key doesn't seem to exist in production, which means that the code within the if statement will never be executed and will jump straight to the $user variable, which of course won't be set (that happens only if the if gets executed.

    Take a review on your config and start from there.

    Good luck!