Laravel-admin 自定義匯出表單

Heinirchs發表於2018-09-26

官方匯出文件

laravel-admin 自帶的匯出 excel 會匯出與此模型關聯的其他資料。所以參考官方文件調整程式碼

B表:id,user_id,start,start_time[時間戳],address,province,city,district,created_at

A表: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){
                        switch ($keyName) {
                            case 'start_time' :
                                $arr[] = date('Y-m-d H:i:s', array_get($item, $keyName));
                                break;
                            case 'start' :
                                $start = array_get($item, $keyName);
                                switch ($start) {
                                    case 1 :
                                        $start = "已釋出";
                                        break;
                                    default:
                                        $start = "未釋出";
                                        break;
                                }
                                $arr[] = $start;
                                break;
                            case 'address' ://組裝多條件
                                $arr[] = array_get($item, 'province').array_get($item, 'city').array_get($item, 'district').array_get($item, 'address');
                                break;
                            case 'province' : break;//過濾掉不處理引數
                            case 'city' : break;//過濾掉不處理引數
                            case 'district' : break;//過濾掉不處理引數
                            default:
                                $arr[] = array_get($item, $keyName);
                                break;
                        }
                    }
                    return $arr;
                });
                $rows = collect([$head])->merge($bodyRows);
                $sheet->rows($rows);
            });
        })->export('xls');//.xls .csv ...
    }
}

例項

use App\Admin\Extensions\ExcelExpoter;

    .
    .
    .
    $excel = new ExcelExpoter();
    $excel->setAttr(['id', '使用者','釋出時間','時間','狀態','詳細地址'], ['id', 'user.username','created_at','start_time','start','address']);
    $grid->exporter($excel);
本作品採用《CC 協議》,轉載必須註明作者和本文連結
Wild programmer

相關文章