laravel-admin 自定义导出表单
2024-08-30 09:02:54
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);
最新文章
- 360浏览器 默认IE7渲染的解决
- python虚拟机中的异常流控制
- html_博客博主
- GitHub上下载源代码的方法
- windows 安装 go语言
- Spring中使用Jcaptcha实现校验码验证
- 【周期串】NYOJ-1121 周期串
- PostgreSQL的 initdb 源代码分析之二十二
- iOS中UITextView键盘回收
- Notepad++使用教程
- Centos7 下Jenkins 安装
- .NET 文件格式相关开源项目
- java实现将指定文件夹里所有文件路径输出到指定文件作为参数化文件给lr脚本使用
- 从零开始学spring cloud(十一) -------- hystrix监控
- Python基础(上)
- AGC 018E.Sightseeing Plan(组合 DP)
- StringBuffer 清除内容
- js 时间的国际化处理
- GreenPlum学习笔记:新手入门命令
- Day01 html详解