Obtain hashed password for using in UserTableSeeder

Hi

There is a default ‘admin’ given in the UserTableSeeder file along with its hashed password. I want to modify the admin password/create another default user. How do I obtain the hashed password for using in the file?

Thanks in advance :smiley:

Hi,

there 2 ways laravel can create record on fresh install, 1 is model factories files to which can be located in database/factories/[ModelFacrotyName].php
and other is in the database seeder files which can be located in database/seeders/[DatabaseSeeder].php

these are two places, once you figure out which file you need to update in order to get what you want, you can use Illuminate\Support\Facades\Hash::make('password')to get hashed password, and then save that password to your database. if you still have some issues figuring it out then i suggest paste some code snippet or something for us to get better idea on what you have done, and what you intend to do.

Regards

1 Like

@syed That’s great. Thanks a lot :slight_smile: The method i tried is that i displayed the hashed password in my view using the following code:

image

and copied that hash and pasted it in my UsersTableSeeder file. This works great but I just hope this is not a wrong/bad way to achieve what I wanted to.

Hi,

i would suggest not to put hash directly in the seeder, here is my sample code for user seeds

DB::table('users')->insert([
        [
            'username' => 'admin', 
            'email' => 'admin@example.com', 
            'password' => Hash::make('admin'),
            ...
        ],
        ...
]

i think hash uses app_key that you can generate anytime with artisan, but in your case i think that is already done

Regards

1 Like

The thing is, Laravel-Core-UI-Admin-Panel stored the hash directly in the seeder. Going by your above solution, wouldn’t the password be exposed if I write it in the format: ‘password’ => Hash::make(‘admin’) where ‘admin’ is the password in this case?

i am not sure about Laravel-Core-UI, i use their vuejs version mostly,

about your question in the last message, i mean it depends on how you manage, usually just to be sure i get default username, email, password from environment file

so that way password would be whatever you set in the env, and we know that .env files are gitignored, whoever pulls the code, they would need to set their own env variables

here is how i do it in my projects

'username' => config('app.admin_username'), 
'email' => config('app.admin_email'), 
'password' => Hash::make(config('app.admin_password')),

and in the config/app.php i would define

'admin_email' => env('ADMIN_EMAIL'),
'admin_username' => env('ADMIN_USERNAME'),
'admin_password' => env('ADMIN_PASSWORD'),

and then finally in the .env file

ADMIN_EMAIL=admin@example.com
ADMIN_USERNAME=admin
ADMIN_PASSWORD=admin

it really depends on how you want to manage it, for me, i prefer to use env variables for tasks such as this.

but with this make sure to set APP_DEBUG to false in .env

because (you may already know this but) incase there is an error, laravel shows everything (all the configurations) in stack trace, if debug is on,

1 Like

@syed Thank you so much :slight_smile: This is great and it has clarified all my doubts. :smiley: :pray: