Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Khin Zin Zin Thinn's avatar

Laravel Excel - Mapping Doesn't Bring Any Data (Doesn't Work)

Hello forks! I am using the Laravel Excel package to export a list of transactions with the relationships. In the example below, I have used two methods ('headings' and 'map'). When I export, I receive the data for headings but no data for mapping. Please help me, folks. Thank you so much in advance.

Export / TransactionsExport.php

<?php

namespace App\Exports;

use App\Models\Transaction;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;

class TransactionsExport implements WithHeadings, WithMapping
{
    /**
    * @var Transaction $transaction
    */
    public function map($transaction): array
    {
        dd('work?');
        return [
            $transaction->invoice_no,
            $transaction->tenant->name,
            $transaction->end_date,
        ];
    }

    public function headings(): array
    {
        return [
            'Invoice No.',
            'Tenant Name',
            'End Date',
        ];
    }
}

TransactionController.php

use App\Exports\TransactionsExport;

// Export as excel
    public function export() {
        return Excel::download(new TransactionsExport, 'transactions.xlsx');
    }
0 likes
7 replies
Khin Zin Zin Thinn's avatar

@Sinnbeck Thank you so much. Since the documentation doesn't clearly mention that we need to use the collection() or query() before mapping, I didn't know what to do as a beginner. When I tried adding them before mapping, it works now. Thank you so much again.

Khin Zin Zin Thinn's avatar

@Sinnbeck Yes, I was able to export the collection(), query() as the documentation explained. I was only stuck with the mapping. However, thanks for your answer. Have a nice day.

Sinnbeck's avatar

@Khin Zin Zin Thinn Same to you. Please remember to mark a best answer to set the thread as solved.

1 like

Please or to participate in this conversation.