1

I need to put the first column is the number of row and the 1st and 2nd row I need to put the title of excel (not the data, it looks like the header of page). What should I do for this case? I've tried using map($row, $index) to get number index+1 but it not supported for laravel-excel. Here is it my current code

public function map($row): array
    {
        $fields = [
            //i need in this column should give number 1,2,3,...
            Date::dateTimeToExcel($row->date),
            $row->title,
            $row->link,
            $row->file,
            $row->description,
        ];
        return $fields;
    }

public function headings(): array
    {
        $fields = [
            'Date',
            'Title',
            'Link',
            'Evidence',
            'Desc',
        ];
        return $fields;
    }
Ray Coder
  • 1,022
  • 3
  • 15
  • 30

2 Answers2

0
protected $index = 0;

public function map($row): array
{
     $fields = [
         ++$this->index,
         Date::dateTimeToExcel($row->date),
         $row->title,
         $row->link,
         $row->file,
         $row->description,
     ];
     return $fields;
}
0

We can utilize the map function to create numbering in Excel exports by adding an additional variable to hold the numbers.

protected $index = 0;

public function map($employee): array
{
    return [
        ++$this->index,
        $employee->client->name,
        $employee->account->name,
        Carbon::parse($employee->created_at)->format('m/d/Y H:i'),
    ];
}

public function headings(): array
{
    return [
        'NO',
        'UNIT',
        'NAME',
        'DATE',
    ];
}