Home > Back-end >  Seeders in this scenario
Seeders in this scenario

Time:10-27

I have an email configured in the services.admin.key that has the email of the admin of the app.

The app only has one admin. Do you know the best approach so that someone who has access to the code from Git could run a command to configure the user? For example, if its a table with three columns:

name: admin
email: the email configured in`services.admin.key`
password: sometestpass

How can I allow someone who has access to the project to run some command to create one user with these details? Do you know what the best approach for this is? I doubt how to approach this properly if it should be with a seeder or another approach?

For example, I have this method on the user model that I use in some views to check if the current user is an admin.

 public function isAdministrator ()
 {
     if ($this->email == config('services.admin.key')) 
     {
            return true;
     }

     return false;
 }

CodePudding user response:

If you talk about fresh project on local database, it would be nice to get ready seeder for admin user.(not remote database). let's say I got access to git repo I could clone project, and run composer install for dependencies and than PHP command php artisan migrate --seed it would be good to have seeder ready for creating admin. To make seeder you need just php artisan make:seed AdminSeeder and after write in AdminSeeder file something like:

User::create(['name' => 'admin', 'email' => config('services.admin.key')),'password' => bcrypt('sometestpass'),]);

than in Database/seeders/DatabaseSeeder.php write $this->call(AdminSeeder::class); it's all. now when someone get fresh project it does 3 commands to make everything ready.

  1. composer install
  2. php artisan migrate --seed
  3. php artisan key:generate

hope I understood right way what you needed.

  • Related