Dact-admin如何导出模型关联字段的值

现在使用dact-admin2对数据展示用到了模型关联,然后导出excel的时候无法导出我模型关联表中的字段的值.
例如,a表字段1,2,3,b表字段,4,5,6.数据展示页面展示数据字段为1,2,3,5.数据可以正常显示,但是导出excel无法导出5字段这一列的值,有什么办法可以解决吗?不想重新自定再写一个导出excel

    protected function grid()
    {
        $languageId = 1;
        $locale = config('app.locale');
        switch ($locale) {
            case 'zh_CN':
                $languageId = 1;
                break;
            case 'en':
                $languageId = 2;
                break;
        }
        return Grid::make(new ShopItemSales(), function (Grid $grid) use($languageId){
            $grid->model()->where('type', 2)->with(['goodsPrototype.i18nSentence.translations' => fn($q) => $q->where('language_id', $languageId)]);
//            $grid->column('id', 'ID')->sortable();
            $grid->column('date', 'date')->display(function ($date) {
                return $date;
            });

            $grid->column('date_type', 'date type')->display(function ($type) {
                return $type === 'D' ? 'D' : ($type === 'W' ? 'W' : 'M');
            });

            $grid->column('shop_id', 'shop ID');
            $grid->column('translation', 'shop name')->display(function () use ($languageId) {
                return $this->goodsPrototype?->i18nSentence?->translations
                    ->where('language_id', $languageId)
                    ->first()?->translation ?? '未翻译';
            });
            $grid->column('shop_count', 'shop Number');

            // 禁用创建按钮
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->disableBatchDelete();
            $grid->disableRowSelector();
            $grid->scrollbarX();
            // 设置表格工具栏
            $grid->toolsWithOutline(false);
            $grid->option('bordered', true);
            $grid->option('striped', false);

            $date_type = request('date_type', 'D');
            $start_date = request('date.start');
            $end_date = request('date.end');
            $params =[
                'type'=>2,
                'date_type'=>$date_type,
                'start_date'=>$start_date,
                'end_date'=>$end_date,
                'language_id'=>$languageId
            ];
            // 启用导出功能
            $grid->export()->filename('SHOP_ITEM_SALES_cash_purchase_'.date('Ymd'));
            //$grid->export(new  ShopItemSalesExport($params))->filename('SHOP_ITEM_SALES_cash_purchase_'.date('Ymd'));
            // 启用分页
            $grid->paginate(10);

            // 添加筛选器
            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->equal('date_type',  __('kpi.type'))->select(function () {
                    return (new ShopItemSales())->getDateTypeOptions();
                })->default('D')->width(2);
                $filter->between('date', __('kpi.date_range'))->date()->default([
                    'start' => request('date.start', Carbon::now()->subDays(30)->toDateString()),
                    'end'   => request('date.end', Carbon::now()->toDateString()),
                ])->width(3);
                $filter->expand(true); // 默认展开筛选框
                $filter->panel(); // 使用面板样式

            });
        });
    }

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
Mutoulee
最佳答案

你是怎么导出的?代码呢?

正常来说启用表格的导出功能本身就包含了关联关系。

数据导出《Dcat Admin 中文文档》

1个月前 评论
蜗牛啊蜗牛 (楼主) 1个月前
蜗牛啊蜗牛 (楼主) 1个月前
Mutoulee (作者) 1个月前
讨论数量: 12
Mutoulee

你是怎么导出的?代码呢?

正常来说启用表格的导出功能本身就包含了关联关系。

数据导出《Dcat Admin 中文文档》

1个月前 评论
蜗牛啊蜗牛 (楼主) 1个月前
蜗牛啊蜗牛 (楼主) 1个月前
Mutoulee (作者) 1个月前

我试了下,可以

file

file

1个月前 评论
蜗牛啊蜗牛 (楼主) 1个月前
提桶跑路了 (作者) 1个月前

应该是你的translation要在grid->model里面才行,强塞进去display,导出的话就是空的

1个月前 评论

file 我看你这里已经查了一遍,按道理$grid->column('goodsPrototype.i18nSentence.translations[0].translation', 'shop name') 能拿到

1个月前 评论
蜗牛啊蜗牛 (楼主) 1个月前
蜗牛啊蜗牛 (楼主) 1个月前
提桶跑路了 (作者) 1个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!