首页 > 解决方案 > 如何使用 maatwebsite excel 导出相关表中的数据?

问题描述

在带有 "maatwebsite/excel": "^3.1" 的 Laravel 8 中,我使用某些枚举列必须具有标签而不是枚举值的功能导出表行,例如:

public function collection()
{
    $adsIdList = [7,1,5];
    return Ad
        ::getById($adsIdList)
        ->get()
        ->map(function ($adItem)  {
            $a = Carbon::createFromFormat('Y-m-d', $adItem->expire_date);
            $is_past = $a->isPast();
            $retAdItem = [
                'id'     => $adItem->id,                                // A
                'title'     => $adItem->title,                          // B
                'slug'    => $adItem->slug,                             // C
                'phone_display' => $adItem->phone_display ? 'Y' : 'N',  // D
                'has_locations' => $adItem->has_locations  ? 'Y' : 'N', // E
                'status' => Ad::getStatusLabel($adItem->status),        // F
                'price' => $adItem->price,                              // G
                'ad_type' => Ad::getAdTypeLabel($adItem->ad_type),      // H
                'expire_date' => $adItem->expire_date,                  // I
                'is_past' => $is_past,                                  // ?

我还在上面的 app/Exports/AdsExport.php 文件中添加了一些计算字段。

但是如果有办法从相关表中添加数据呢?任何广告都可以有多个带有多个字段的 ad_locations...稍后我将需要编写导入功能以在导出文件中生成...

谢谢!

标签: laravelmaatwebsite-excel

解决方案


推荐阅读