官方导出文档

laravel-admin自带的导出excel会导出与此模型关联的其他数据。所以参考官方文档调整代码

文章表:id,title,user_id

用户表:id,username

//文章模型关联用户
public function user(){
return $this->belongsTo(User::class, 'user_id', 'id');
}
//ExcelExporter.php
<?php
namespace App\Admin\Extensions; use Encore\Admin\Grid;
use Encore\Admin\Grid\Exporters\AbstractExporter;
use Maatwebsite\Excel\Facades\Excel; class ExcelExpoter extends AbstractExporter
{
protected $head = [];
protected $body = [];
public function setAttr($head, $body){
$this->head = $head;
$this->body = $body;
} public function export()
{
Excel::create('Filename', function($excel) {
$excel->sheet('Sheetname', function($sheet) {
// 这段逻辑是从表格数据中取出需要导出的字段
$head = $this->head;
$body = $this->body;
$bodyRows = collect($this->getData())->map(function ($item)use($body) {
foreach ($body as $keyName){
$arr[] = array_get($item, $keyName);
}
return $arr;
});
$rows = collect([$head])->merge($bodyRows);
$sheet->rows($rows);
});
})->export('xls');
}
}

使用方法:

            $excel = new ExcelExpoter();
$excel->setAttr(['id', '标题', '作者'], ['id', 'title', 'user.username']);
$grid->exporter($excel);

最新文章

  1. 360浏览器 默认IE7渲染的解决
  2. python虚拟机中的异常流控制
  3. html_博客博主
  4. GitHub上下载源代码的方法
  5. windows 安装 go语言
  6. Spring中使用Jcaptcha实现校验码验证
  7. 【周期串】NYOJ-1121 周期串
  8. PostgreSQL的 initdb 源代码分析之二十二
  9. iOS中UITextView键盘回收
  10. Notepad++使用教程
  11. Centos7 下Jenkins 安装
  12. .NET 文件格式相关开源项目
  13. java实现将指定文件夹里所有文件路径输出到指定文件作为参数化文件给lr脚本使用
  14. 从零开始学spring cloud(十一) -------- hystrix监控
  15. Python基础(上)
  16. AGC 018E.Sightseeing Plan(组合 DP)
  17. StringBuffer 清除内容
  18. js 时间的国际化处理
  19. GreenPlum学习笔记:新手入门命令
  20. Day01 html详解

热门文章

  1. IDEA eclipse 控制台日志输出到文件
  2. spring cloud服务注册与发现无法发现的可能原因
  3. php-循环普通数组和关联数组
  4. Partition算法及Partition算法用于快速排序
  5. 细谈HTML解析模块
  6. 【C】貌似不友好的scanf()
  7. smali语法积累记录
  8. 并不对劲的BJOI2019
  9. CodeForces 1103E. Radix sum
  10. Asset Catalog Help (五)---Migrating an iOS App Icon Set or Launch Image Set