rap2hpoutre / fast-excel

🦉 Fast Excel import/export for Laravel
MIT License
2.11k stars 247 forks source link

can't download excel with date in query #256

Open hergimerc opened 2 years ago

hergimerc commented 2 years ago

I'm using DB::select to query data, and then change it to collection and then download. The problem is, if there's a date in my where statement, even if it was hardcoded in the query, it failed to download the excel and returns This site can’t be reached error (ERR_INVALID_RESPONSE). If there's no date in where statement, the excel can be download successfully.

Here's my code. For brevity, I omit columns in select statement.

This code works.

$q = DB::select("
        SELECT
            *
        FROM
            edukasi_ihprepaid_master
            LEFT JOIN areas ON edukasi_ihprepaid_master.cwitel = areas.cwitel
            LEFT JOIN users ON edukasi_ihprepaid_master.user_call = users.id
            LEFT JOIN hasil_call_ihprepaid ON edukasi_ihprepaid_master.hcall_id = hasil_call_ihprepaid.id
        WHERE
            edukasi_ihprepaid_master.user_call = 60
            AND hcall_id = 1
    );

        $collection = collect($q);
        return (new FastExcel($collection))->download('ihprepaid_reports.xlsx');

While this codes don't work because there's a date filter in where statement.

$q = DB::select("
        SELECT
            *
        FROM
            edukasi_ihprepaid_master
            LEFT JOIN areas ON edukasi_ihprepaid_master.cwitel = areas.cwitel
            LEFT JOIN users ON edukasi_ihprepaid_master.user_call = users.id
            LEFT JOIN hasil_call_ihprepaid ON edukasi_ihprepaid_master.hcall_id = hasil_call_ihprepaid.id
        WHERE
            edukasi_ihprepaid_master.user_call = 60
            AND hcall_id = 1
            AND tgl_call::date BETWEEN '2022-01-11'
            AND '2022-01-12'"
    );

        $collection = collect($q);
        return (new FastExcel($collection))->download('ihprepaid_reports.xlsx');