2
votes

I have a simple query that generates files using the maatwebsite

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use App\MyDB;
use Auth;


 class ReportExport implements FromCollection
{
    public function collection()
    {
        $email = Auth::user()->email;
        return MyDB::MyFunction($email)
                ->select('Reference_Number')->get();
    }
}

everything is working fine, but how can i add headers?

I tried looking at the documentation but it confused me more. https://laravel-excel.maatwebsite.nl/docs/3.0/export/mapping

1

1 Answers

4
votes

I must also admit. The documentation on Laravel Excel tries to mess with your head. You can add headings by adding a headings function to your export class:

use Maatwebsite\Excel\Concerns\WithHeadings;

 class ReportExport implements FromCollection, WithHeadings
{
    public function collection()
    {
        $email = Auth::user()->email;
        return MyDB::MyFunction($email)
                ->select('Reference_Number')->get();
    }

    public function headings(): array
        {
            return [
                'Heading 1',
                'Heading 2',
                'Heading 3',  
            ];
        }
}