PHP code example of eric-strive / hyperf-tool
1. Go to this page and download the library: Download eric-strive/hyperf-tool 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/ */
eric-strive / hyperf-tool example snippets bash
php bin/hyperf.php vendor:publish eric-strive/hyperf-tool
bash
php bin/hyperf.php make:controller TestUserController TestModel 测试我的功能
bash
php bin/hyperf.php make:request RoleRequest roles
$excelImport = new Import($file, $startRow, $endVerifyLine);
$excelImport->process(function ($saveData) {
// 处理$saveData
}, $format);
$file :导入的文件对象,UploadedFile对象
$startRow:文件开始读取的行,如果需要过滤前面的数据可以使用
$endVerifyLine:验证的列,这里防止文件后面空行过多导致报错,验证的行为空时直接终止取数
$saveData:获取的数据
$format:数据对应格式,具体行对应的字段配置
eg:
[
'email' => 2,
'phone' => 1,
'user_name' => 0,
'real_name' => 3,
'job_number' => 0,
]
这里是字段分别对应的列,逻辑会自动根据对应的列来处理数据
Download::downloadCsv($fileName, $data, true),$format);
$fileName:导出文件名称
$data:导出数据,这里可以是数组和对象,对象需要时model的对象
$format:数据处理的格式
eg:
[
[
'label' => "真实姓名",
'value' => 'real_name',
'width' => 15,
],
[
'label' => "创建时间",
'value' => 'created_at',
'width' => 20,
],
[
'label' => "类型",
'value' => static function ($model) use ($employeeType) {
return getExistParam($employeeType, $model->type);
},
'width' => 5,
],
]
上面配置:
label:csv头信息
value:取的对象或数组的值,可以是函数
width:该列的宽度,根据实际调整
Download::downloadCsv('test.csv', [[
'real_name'=>'王五',
'created_time'=>'2020-01-02 10:02:12',
'type'=>'测试',
],[
'real_name'=>'张三',
'created_at1'=>'2020-01-23',
'type'=>'类型',
]], [
[
'label' => "真实姓名",
'value' => 'real_name',
'width' => 15,
],
[
'label' => "创建时间",
'value' => 'created_time',
'width' => 20,
],
[
'label' => "类型",
'value' => static function ($model) {
return $model['type'] . '测试类型';
},
'width' => 5,
],
]);