Dcat Admin导出功能

1、安装 larave-excel

composer require maatwebsite/excel

注意 laravel 版本号,目前支持 5.8-8 以及以上的版本,默认安装的是 3.1 版本。 

fileName = !empty($fileName) ? $fileName : $this->getFilename();

        $this->fileName .= '.xlsx'; //拼接下载文件名称

        $this->titles = ['id' => 'id', 'user_id' => '所属用户' ,'created_at'=>'创建时间','updated_at'=>'更新时间'];
        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
    {

        // 直接返回row 导出全部字段 dd($row);
        // return $row;

        // TODO: Implement map() method.
        return [
            $row['id'],
            $row['user_id'],
            $row['created_at'],
            $row['updated_at'],
        ];
    }
}

可以看出,我们仅仅只需要关注 map 和 title 即可。

2、在控制器中使用

$grid->export(new Export('投产比'));

3、数据仓库

usePaginate(true);
        $current_page = $model->getCurrentPage();
        $per_page = $model->getPerPage();

        $is_export = $model->filter()->input('_export_', null);
        $date_start = $model->filter()->input('date.start', null);
        $date_end = $model->filter()->input('date.end', null);

        $where = [];
        if ($date_start && $date_end) {
            $where[] = ['at_date', '>=', date('Ymd', strtotime($date_start))];
            $where[] = ['at_date', '<=', date('Ymd', strtotime($date_end))];
        }
        

        //处理分页显示的日期范围
        if ($is_export == Grid\Exporter::SCOPE_ALL) {

            $list = DB::connection('mysql')->table('production_ratio')
                ->where($where)
                ->orderByDesc('at_date')
                ->get()->map(function ($value) {
                    return (array)$value;
                })->toArray();

            foreach ($list as &$value) {
                $value['rol'] = $value['cost'] > 0 ? number_float($value['profit'] / $value['cost']) : 0;
            }
            return $list;

        } else {
            $list = DB::connection('mysql')->table('production_ratio')
                ->where($where)
                ->orderByDesc('at_date')
                ->skip(($current_page - 1) * $per_page)
                ->take($per_page)
                ->get()->map(function ($value) {
                    return (array)$value;
                })->toArray();
        }

        foreach ($list as &$value) {
            $value['rol'] = $value['cost'] > 0 ? number_float($value['profit'] / $value['cost']) : 0;
        }
        
        $total = DB::connection('mysql')->table('production_ratio')
            ->where($where)
            ->count();
        
        return $model->makePaginator(
            $total,
            $list
        );

    }


}

你可能感兴趣的:(laravel,dcat,admin,php,laravel)