msslgomez
1 week ago

Laravel Excel not downloading

Posted 1 week ago by msslgomez

No matter what I try I can't get the file to download at all

This is the Export file

class ReportExcel implements FromArray, withHeadings
{
    use Exportable;

    protected $items;

    public function __construct(array $items)
    {
        $this->items = $items;
    }

    public function headings(): array
    {
        return ["Opcion 1", "Opcion 2", "Opcion 3", "Nombres", "Apellidos", "Telefono", "Correo", "Pais", "Pais Otro", "Edad", "Adventista", "Estudia la Biblia", "Peticion", "Fecha"];
    }

    public function array(): array
    {
        $data = [];

        foreach ($this->items as $item) {
            foreach ($item as $i) {
                $option1 = ($i->option_1 == 0) ? 'No' : 'Si';
                $option2 = ($i->option_2 == 0) ? 'No' : 'Si';
                $option3 = ($i->option_3 == 0) ? 'No' : 'Si';

                $country = Country::where('id', $i->country_id)->pluck('name')->first();

                $data[] = [$option1, $option2, $option3, $i->name, $i->lastname, $i->phone, $i->email, $country, $i->country_other, $i->age, $i->adventist, $i->study, $i->petition, $i->date_answered];
            }

        }

        return $data;
    }
}

Route, I'm using a post here because I need to send the data I need to use in order to send the correct data to the export file

 Route::post('/download', '[email protected]');

Method in Controller

public function downloadExcel(Request $request)
    {
        if ($request->country == null) {
            $item = Form::get(['option_1', 'option_2', 'option_3', 'name', 'lastname', 'phone',
                'email', 'country_id', 'country_other', 'age', 'adventist', 'study', 'petition', 'date_answered']);
        } else {
            $item = Form::where('country_id', $request->country)->orderByDesc('created_at')
                ->get(['option_1', 'option_2', 'option_3', 'name', 'lastname', 'phone', 'email', 'country_id', 'country_other', 'age', 'adventist', 'study', 'petition', 'date_answered']);
        }

        $items[] = $item;

//     Neither of these work, they just display a window inside the page with the data in text, not formatted or anything 

        return (new ReportExcel($items))->download('invoices.csv');

//        return Excel::download(new ReportExcel($items), 'reporte.csv');

    }

What am I doing wrong? How can I get this to download a file?

Please sign in or create an account to participate in this conversation.