Almost every business app ends up with two requests: "download this as Excel" and "give me a PDF invoice". Laravel handles both with packages: Laravel Excel (maatwebsite/excel) for spreadsheets and DomPDF or Browsershot for PDF.

bash
composer require maatwebsite/excel
composer require barryvdh/laravel-dompdf

An export class

An export is a class that lists what it can do through interfaces. FromQuery supplies the data, WithHeadings the first row, WithMapping the format of each row.

php
<?php

namespace App\Exports;

use App\Models\Order;
use Illuminate\Database\Eloquent\Builder;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class OrdersExport implements FromQuery, WithHeadings, WithMapping
{
    use Exportable;

    public function __construct(private int $year) {}

    public function query(): Builder
    {
        return Order::query()
            ->with('customer')
            ->whereYear('created_at', $this->year);
    }

    public function headings(): array
    {
        return ['ID', 'Customer', 'Total', 'Date'];
    }