Laravel-excel: Moving to next row in map method

Created on 14 Jun 2019  路  4Comments  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

Versions

  • PHP version:
  • Laravel version:
  • Package version:

Description

Additional Information

Any additional information, configuration or data that might be necessary to reproduce the issue.
I am using Laravel version 5.5 and Laravel-Excel 3.1
I am trying to format my excel file according to my specifications using an array.
I am using the 'map' method included and it is working fine. It is below

`public function map($ticketsData): array
    {

        $row = [
            $ticketsData->id,
            $ticketsData->date,
            $ticketsData->status,
            $ticketsData->total,
            $ticketsData->discount,
            $ticketsData->paid,
        ];
        return $row;
    }`

I want to move to next row in this function to output nested array data, but I cannot seem to do so. How can it be done

Most helpful comment

Not sure I really understand what you want to do, but the map() function can return multiple rows.

public function map($ticketsData): array
    {

        $row1 = [
            $ticketsData->id,
            $ticketsData->date,
            $ticketsData->status,
            $ticketsData->total,
            $ticketsData->discount,
            $ticketsData->paid,
        ];

        $row2 = [
            //... more data
      ];

        return [
            $row1,
            $row2,
        ];
    }

All 4 comments

Can you elaborate on it please?

Not sure I really understand what you want to do, but the map() function can return multiple rows.

public function map($ticketsData): array
    {

        $row1 = [
            $ticketsData->id,
            $ticketsData->date,
            $ticketsData->status,
            $ticketsData->total,
            $ticketsData->discount,
            $ticketsData->paid,
        ];

        $row2 = [
            //... more data
      ];

        return [
            $row1,
            $row2,
        ];
    }

Not sure I really understand what you want to do, but the map() function can return multiple rows.

public function map($ticketsData): array
    {

        $row1 = [
            $ticketsData->id,
            $ticketsData->date,
            $ticketsData->status,
            $ticketsData->total,
            $ticketsData->discount,
            $ticketsData->paid,
        ];

        $row2 = [
            //... more data
      ];

        return [
            $row1,
            $row2,
        ];
    }

This is exactly what I requested and worked like a charm. Thanks, you managed to solve the problem despite me not being clear in explaining it.
This should be somewhere in the docs.
Thanks again!

@abid1208 Glad you managed to solve your problem. Feel free to PR an update to the docs! :)
Also don't forget to send us a postcard! (https://docs.laravel-excel.com/3.1/getting-started/license.html#postcardware) :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lucatamtam picture lucatamtam  路  3Comments

octoxan picture octoxan  路  3Comments

thearabbit picture thearabbit  路  3Comments

matthewslouismarie picture matthewslouismarie  路  3Comments

muhghazaliakbar picture muhghazaliakbar  路  3Comments