only admin can create a new user in laravel code example

Example: laravel create default admin user

Add to the .env file:
  ADMIN_NAME="John Doe"
  [email protected]
  ADMIN_PASSWORD=secure_pa55word

Add to the .env.example file:
  ADMIN_NAME=
  ADMIN_EMAIL=
  ADMIN_PASSWORD=

Create 'admin.php' inside the \config\ folder and paste:
  <?php
  return [

      /*
      |--------------------------------------------------------------------------
      | Default admin user
      |--------------------------------------------------------------------------
      | Default user will be created at project installation/deployment
      */

      'admin_name' => env('ADMIN_NAME', ''),
      'admin_email' => env('ADMIN_EMAIL', ''),
      'admin_password' =>env('ADMIN_PASSWORD', '')
      ];
    
Create the database seeder:
	php artisan make:seeder UsersTableSeeder

Edit the seeder file:
  <?php
  use Illuminate\Database\Seeder;
  use App\Models\User;

  class UserTableSeeder extends Seeder
  {
      /**
       * Run the database seeds.
       *
       * @return void
       */
      public function run()
      {
          if(config('admin.admin_name')) {
              User::firstOrCreate(
                  ['email' => config('admin.admin_email')], [
                      'name' => config('admin.admin_name'),
                      'password' => bcrypt(config('admin.admin_password')),
                  ]
              );
          }
      }
  }

On 'databaseSeeder.php' add:
	$this->call(UsersTableSeeder::class);

Run:
	php artisan db:seed --force"

Tags:

Misc Example