当前位置: 首页 > 工具软件 > laravel-tags > 使用案例 >

laravel-admin自定义导出关联的数据

郭远
2023-12-01

 

1.在在App\Admin\Extensions 增加 CustomExporter.php;

<?php

namespace App\Admin\Extensions;
use Encore\Admin\Grid;
use Illuminate\Support\Arr;
use Encore\Admin\Grid\Exporters\AbstractExporter;

class CustomExporter extends AbstractExporter
{
    protected $filename = 'table';
    protected $head = [];
    protected $body = [];

    public function __construct($filename = 'table', $head = null, $body = null, Grid $grid = null)
    {
        $this->filename = $filename;
        $this->head = $head;
        $this->body = $body;
        parent::__construct($grid);
    }

    /**
     * {@inheritdoc}
     */
    public function export()
    {
        $titles = [];
        $filename = $this->filename.'.csv';
        $data = $this->getData();
        if (!empty($data)) {
            $columns = array_dot($this->sanitize($data[0]));
            $titles = array_keys($columns);
        }
        $output = self::putcsv(($this->head == []) ? array_keys($columns) : $this->head);
        if($this->body == []) {
            foreach ($data as $row) {
                $row = array_only($row, $titles);
                $output .= self::putcsv(array_dot($row));
            }
        }else {
            foreach ($this->body as $row) {
                $output .= self::putcsv(array_dot($row));
            }
        }
        $headers = [
            'Content-Encoding'    => 'UTF-8',
            'Content-Type'        => 'text/csv;charset=UTF-8',
            'Content-Disposition' => "attachment; filename=\"$filename\"",
        ];
        response(rtrim($output, "\n"), 200, $headers)->send();
        exit;
    }

    /**
     * Remove indexed array.
     *
     * @param array $row
     *
     * @return array
     */
    protected function sanitize(array $row)
    {
        return collect($row)->reject(function ($val) {
            return is_array($val) && !Arr::isAssoc($val);
        })->toArray();
    }

    /**
     * @param $row
     * @param string $fd
     * @param string $quot
     *
     * @return string
     */
    protected static function putcsv($row, $fd = ',', $quot = '"')
    {
        $str = '';
        foreach ($row as $cell) {
            $cell = str_replace([$quot, "\n"], [$quot.$quot, ''], $cell);
            if (strstr($cell, $fd) !== false || strstr($cell, $quot) !== false) {
                $str .= $quot.$cell.$quot.$fd;
            } else {
                $str .= $cell.$fd;
            }
        }
        return substr($str, 0, -1)."\n";
    }
}

2.在相关控制器中调用即可

//指定导出
if (isset($_GET['_export_'])) {
    //自定义报表导出格式
    $supervision_query = SupervisionUser::with([
        'miniProgramUser',
        'supervisionRecommend.recommendUser'
    ])->whereNotIn('is_delete', [1]);
    if (isset($_GET['status']) && !empty($_GET['status'])) {
        $supervision_query->where('status', $_GET['status']);
    }
    if (isset($_GET['profession']) && !empty($_GET['profession'])) {
        $supervision_query->where('profession', $_GET['profession']);
    }
    if (isset($_GET['name']) && !empty($_GET['name'])) {
        $supervision_query->where('name', $_GET['name']);
    }
    $cards = $supervision_query->get();
    $head = ['序号', '姓名', '手机号', '省', '市', '区', '是否是机器号', '审核状态', '职位', '店铺类型', '推荐人/id', '申请时间'];
    $body = [];
    foreach ($cards as $card) {
        switch ($card['type']) {
            case 1:
                $card['type'] = 'C店';
                break;
            case 2:
                $card['type'] = 'A店';
                break;
            case 3:
                $card['type'] = 'B店';
                break;
            default:
                $card['type'] = 'C店';
                break;
        }
        switch ($card['status']) {
            case 1:
                $card['status'] = '通过';
                break;
            case 2:
                $card['status'] = '未通过';
                break;
            case 3:
                $card['status'] = '禁用';
                break;
            case 4:
                $card['status'] = '已到期';
                break;
            default:
                $card['status'] = '未审核';
        }
        switch ($card['is_default']) {
            case 1:
                $card['is_default'] = '是';
                break;
            case 0:
                $card['is_default'] = '否';
                break;
            default:
                $card['is_default'] = '否';
        }
        switch ($card['profession']) {
            case 1:
                $card['profession'] = '项目经理';
                break;
            case 2:
                $card['profession'] = '装修工长';
                break;
            case 3:
                $card['profession'] = '安装服务网点';
                break;
            default:
                $card['profession'] = '暂无分类信息';
        }
        $body[] = [
            $card['id'],
            $card['name'],
            $card['mobile'],
            $card['province'],
            $card['city'],
            $card['area'],
            $card['is_default'],
            $card['status'],
            $card['profession'],
            $card['type'],
            $card['supervisionRecommend']['recommendUser']['name'] . '/' . $card['supervisionRecommend']['recommend_id'],
            $card['created_at']
        ];

    }
    $grid->exporter(new CustomExporter('xxxx列表', $head, $body));
}
 类似资料: