/** * 將系所分到學院中. * * @return void */ protected function classifyDepartment() { $colleges = ['文學院', '理學院', '社會科學學院', '工學院', '管理學院', '法學院', '教育學院', '其他']; foreach (Category::where('category', 'college')->get() as $college) { $index = array_search($college->getAttribute('name'), $colleges, true) + 1; $query = Category::where('category', 'department'); if (8 !== $index) { $query = $query->where('remark', 'like', "{$index}%"); } else { $query = $query->whereIn('remark', ['I001', 'V000', 'F000', 'Z121']); } $college->update(['remark' => $query->orderBy('id')->get()->implode('id', ',')]); } }
/** * 取得教授 collection. * * @param string $professors * @return \Illuminate\Database\Eloquent\Collection */ protected function professors($professors) { $professors = explode(' ', $professors); $result = Category::where('category', 'professor')->whereIn('name', $professors)->get(); // 將尚未有資料的教授新增到資料庫中 foreach (array_diff($professors, $result->pluck('name')->toArray()) as $name) { $result->push(Category::create(['category' => 'professor', 'name' => $name])->fresh()); } return $result; }
/** * Save courses data to database. * * @param array $data * @return bool */ protected function savingData($data) { foreach ($data as $datum) { $department_id = array_search($datum['department'], $this->correspondenceTable) + 22; // prefix 22 DB::beginTransaction(); try { foreach ($datum['courses'] as $course) { if (Course::where('code', '=', $course['code'])->where('professor', '=', $course['professor'])->exists()) { continue; } else { if (null !== $course['dimension']) { $course['dimension'] = Category::where('category', '=', 'courses.dimension')->where('name', '=', $course['dimension'])->first()->getAttribute('id'); } } Course::create(['code' => $course['code'], 'department_id' => $department_id, 'dimension_id' => $course['dimension'], 'name' => $course['name'], 'name_en' => $course['name_en'], 'professor' => $course['professor']]); ++$this->count; } } catch (Exception $e) { DB::rollBack(); $this->error($e->getMessage()); return false; } DB::commit(); } return true; }