【问题标题】:Laravel excel export list in list object maatwebsite/Laravel-Excel列表对象 maatwebsite/Laravel-Excel 中的 Laravel excel 导出列表
【发布时间】:2020-09-04 01:27:54
【问题描述】:

您好,我想将包含所有孩子的人员列表导出到 Excel。我正在使用 maatwebsite/Laravel-Excel 插件,它很容易导出用户集合,但我需要在该行中包含每个孩子的用户。它类似于 [{Name, Age, Gender, Job, Children[{name, age, gender, etc...}], Birthday, etc...}]。以及如何更改 Excel 上显示的数据列的顺序,就像我想切换年龄和性别的数据一样。谢谢!

【问题讨论】:

  • 请分享您已在问题中编写的代码,以便我们更好地帮助您

标签: excel laravel maatwebsite-excel


【解决方案1】:

您可以使用WithMapping https://docs.laravel-excel.com/3.1/exports/mapping.html

<?php
    public function map($user): array
    {
        //create initial array with user data in order that you want columns to be
        $data = [
           $user->name
           ...
        ];
        // the same way you should crate headers (if you have). But instead of values you should create column name
        foreach($user->children as $child){
            array_push($data, $child->name, $child->birthday, $child->...., ..., ...); //and so on
        }

        // returned data should be 1 level array as each item is a row in the table
        return $data;
    }

【讨论】:

  • 它有点工作,但是当 $user->children 有 2 个或更多项目时,只显示一个,为什么会这样?我需要所有这些孩子在其父母内部或下方跨越一行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-20
  • 2019-01-28
  • 2021-05-01
  • 2016-11-23
  • 1970-01-01
  • 2020-03-10
  • 2017-11-12
相关资源
最近更新 更多