jeudi 3 octobre 2019

export an excel with dynamic array in laravel using maatwebsite

I would like to export Excel in Laravel version 5.7. I am using package Maatwebsite Laravel Excel version 3.1. I have an array of persons' information like this:

 $persons = {
        "person 1": [
            {
                "question1": "first name",
                "answer1": "shokouh"
            },
            {
                "question2": "last name",
                "answer2": "dareshiri"
            },
            {
                "questionN": "question N",
                "answerN": "answer N"
            }
        ],
...
        "person M ": [
            {
                "question1": "first name",
                "answer1": "sarah"
            },
            {
                "question2": "last name",
                "answer2": "smith"
            },
            {
                "questionN": "question N",
                "answerN": "answer N"
            }
        ]
    }

and I wanna to export that in such as below excel:

Person 1    Question 1   Answer 1   Question 2   Answer 2   …   Question N   Answer N
Person M    Question M1  Answer M2  Question M2  Answer M2  …   Question MN  Answer MN

This is my export function in Controller:

public function export(Request $request, Tournament $tournament) {

    ... 

    $persons;

    return Excel::download(new ParticipantExport($persons), 'personInformation.xlsx');
}

This is my participantExport in app\Export\participantExport:

class participantExport implements FromArray
{
    protected $invoices;

    public function __construct(array $invoices)
    {
        $this->invoices = $invoices;
    }

    public function array(): array
    {
        return $this->invoices;
    }
}

any body can help me?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire