fileName = $this->fileName.'_'.time().'.xlsx';//拼接下载文件名称 $this->titles = [ 'mobile' => '手机号' , 'name'=>'姓名', 'idcard'=>'身份证', 'sex' => "性别", "college" => "二级学院", "speciality" => "专业", "create_time" => "注册时间", "is_test" => "是否为测试号" ]; parent::__construct(); } public function export() { // TODO: Implement export() method. $this->download($this->fileName)->prepare(request())->send(); exit; } public function collection() { // TODO: Implement collection() method. return collect($this->buildData()); } public function headings(): array { // TODO: Implement headings() method. return $this->titles(); } public function map($row): array { $nameInfo = $this->getSpecialityAndCollegeNameById($row["speciality_id"]); $sex = $row['sex'] == 1 ? "男" : "女"; $test = $row['is_test'] == 1 ? "否" : "是"; // TODO: Implement map() method. return [ $row['mobile'], $row['name'], "'".$row['idcard'].'', $sex, $nameInfo["name"], $nameInfo["speciality_name"], date("Y-m-d H:i:s", $row['create_time']), $test ]; } public function getSpecialityAndCollegeNameById($id) { $value = Cache::store('file')->remember('specialityInfo', 600, function () { //所有专业 $specialityList = Speciality::query()->get(["id", "speciality_name", "secondary_college_id"])->toArray(); if(!empty($specialityList)){ //所有学院 $secondaryCollegeList = SecondaryCollege::query()->get(["id", "name"])->toArray(); if(!empty($secondaryCollegeList)){ $secondaryCollegeList = array_column($secondaryCollegeList, null, "id"); foreach($specialityList as $key => $item){ if(array_key_exists($item["secondary_college_id"], $secondaryCollegeList)){ $specialityList[$key]["name"] = $secondaryCollegeList[$item['secondary_college_id']]['name']; }else{ $specialityList[$key]["name"] = ""; } } } return array_column($specialityList, null, "id"); } }); return [ "name" => $value[$id]["name"], "speciality_name" => $value[$id]["speciality_name"] ]; } }