/** * 確保測試帳號已建立. * * @return void */ protected function ensureTestUsersCreated() { $userList = ['test' => 'student', 'testLab' => 'lab', 'testManager' => 'manager']; foreach ($userList as $username => $role) { if (!User::where('username', $username)->exists()) { factory(User::class)->create(['username' => $username])->roles()->save(Role::where('name', $role)->first()); } } }
/** * Run the database seeds. * * @return void */ public function run() { if (app()->environment(['local', 'testing'])) { foreach (['test' => 'student', 'testLab' => 'lab', 'testManager' => 'manager'] as $key => $value) { if (!User::where('username', $key)->exists()) { factory(User::class)->create(['username' => $key, 'password' => bcrypt('test')])->roles()->save(Role::where('name', $value)->first()); } } } }
/** * Import students account data with excel * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function import(Request $request) { // get tmp xlsx file path $file_path = $request->file('studentData')->getRealPath(); // extract student data from the file uploaded $student_data = Excel::load($file_path)->toArray(); // processing & storing student data foreach ($student_data as $data) { $user = new User(); $user->username = $data['學號']; $user->password = str_random(96); $user->nickname = $data['姓名']; $user->email = $data['信箱']; $user->phone = $data['電話']; $user->group = $data['班級']; $user->save(); $user->roles()->save(Role::where('name', 'student')->first()); } return response()->json(['status' => 0]); }
/** * Run the database seeds. * * @return void */ public function run() { foreach (['manager', 'lab', 'student'] as $role) { Role::firstOrCreate(['name' => $role]); } }