Laravel excel 列表中的导出列表 object maatwebsite/Laravel-Excel

Laravel excel export list in list object maatwebsite/Laravel-Excel

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

您可以使用 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;
    }