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

eggplantSword's avatar

Export excel from controller with a query

This is what I have currently

Export class

class ReportExcel implements FromQuery, withHeadings
{
    use Exportable;

    public function __construct(int $id)
    {
        $this->id = $id;
    }

    public function query()
    {
        return Form::query()->where('country_id', $this->id);
    }

    public function headings(): array
    {
        return ["Opcion 1", "Opcion 2", "Opcion 3", "Nombres", "Apellidos", "Telefono",
            "Pais", "Pais Otro", "Edad", "Adventista", "Estudia la Biblia", "Peticion"];
    }
}
public function downloadExcel(Request $request)
{
    if ($request->country == null) {
        $items = Form::with('country')->get();
        $ex_title = "Reporte_de_Totales";
    } else {
        $items = Form::where('country_id', $request->country)->with('country')->orderByDesc('created_at')->get();
        $country = Country::where('id', $request->country)->pluck('name')->first();
        $ex_title = "Reporte_para_" + $country;
    }

    return (new ReportExcel((int)$request->country))->download($ex_title, \Maatwebsite\Excel\Excel::XLSX);
}

I get all these weird symbols like this https://ibb.co/F5dTdPF

How can I fix it so I actually download an excel file?

0 likes
1 reply
eggplantSword's avatar
eggplantSword
OP
Best Answer
Level 9

The solution is to send the request not through vue but in a simple <a href="download route" /> since this is a binary download. The data above is correct

Please or to participate in this conversation.