1. Go to this page and download the library: Download jobsys/importexport-module library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
class CourseStudentExporter implements FromQuery, WithHeadings, WithMapping
{
use Exportable;
public int $course_id;
public function __construct($course_id)
{
$this->course_id = $course_id;
}
public function query()
{
return Student::where('course_id', $this->course_id);
}
public function headings(): array
{
return [
'学员编号',
'备注',
'标签',
];
}
public function map($row): array
{
return [
land_csv_to_string($row->student_num),
$row->remark,
implode(', ', $row->tags ?: []),
];
}
}
public function export(Request $request, Course $course)
{
return (new CourseStudentExporter($course_id))->download("{$course->name}学员导出.xlsx");
}
/**
* 保存文件并读取文件头
* @param UploadedFile $file
* @return array
*/
public function readHeaders(UploadedFile $file): array