0
votes

Hi I want to export list of people with all theirs childrens in them to excel. I'm using maatwebsite/Laravel-Excel plugin and its quite easy to export users collection but I need to include users every children inside that row. Its something like [{Name, Age, Gender, Job, Children[{name, age, gender, etc...}], Birthday, etc...}]. And how do I change order of the data column displayed on excel like if I want to switch datas on Age and Gender. Thank you!

1
Please share the code you already wrote in your question so we can help you betterChristophe Hubert

1 Answers

0
votes

You can use 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;
    }