PHP code example of jobsys / importexport-module

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/ */

    

jobsys / importexport-module example snippets


"Importexport" => [
     "route_prefix" => "manager",                                                   // 路由前缀
 ]

    // 定义导入字段
    $fields = [
        ['field' => 'name', 'label' => '学员姓名', 'rule' => '生日', 'type' => 'date'],
    ];
   
    // 附属数据,将在后续的 Importer 中一并处理
    $extra_data = ['creator_id' => $this->login_user_id];
   
    // 调用 ImportexportService 的 import 方法进行数据导入
    list($result, $error) = $service->import('学员信息导入', StudentImporter::class, $fields, $extra_data);
 
    

    public function store(array $row, array $extra): void
    {
        // 通过 $row 和 $extra_data 处理数据并存储,可以进行其它的验证或者是操作,如发送通知提醒等。
        Student::updateOrCreate(['course_id' => $extra['course_id'], 'id_card' => $row['id_card']], array_merge($row, $extra));
    }
    

    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
    

      /**
      * 导入数据,如果上传数据中有文件,将文件保存到本地,并返回文件路径与表头,如果不含文件而只有文件路径,则进行数据导入
      * @param string $title 标题
      * @param string $importer 导入器
      * @param array $fields 字段
      * @param array $extra_data 额外数据
      * @return array
        */
        public function import(string $title, string $importer, array $fields, array $extra_data = []): array
    
bash
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config