Search code examples
phplaravelcsvreportmaatwebsite-excel

How to fix "Call to a member function get() on array" : Laravel 5.4


I have a trouble downloading files that I've generated data, from database using maatwebsite/laravel-excel. Error : Call to a member function get() on array.

Before, when I selected data from the database using only one field, the file can generate(.csv). But when I try to query using aliases "as" the data cannot be obtained.

public function getdoc($type)
{
    -- with this query : success --
    //$temp = AlarmV2::select('msisdn')->get();
    $today = "20181008";
    -- with this query : error --
    $temp = DB::select(DB::raw("select regionalid,
    nvl(sum(case when lastactiontype='0' then totalcharge end),0) as creditlimit_usage, 
    count(case when lastactiontype='0' then msisdn end) as creditlimit_rec,
    from alarms_v2
    where alarmdate = '$today'
    -- fatal error exception :Call to a member function get() on array --
    group by regionalid order by regionalid"))->get();

    return Excel::create('datadoc', function ($excel) use ($temp) {
        $excel->sheet('mySheet', function ($sheet) use ($temp) {
            $sheet->fromArray($temp);
        });
    })->download($type);
}

Solution

  • $temp = DB::select(DB::raw("select regionalid,
        nvl(sum(case when lastactiontype='0' then totalcharge end),0) as creditlimit_usage, 
        count(case when lastactiontype='0' then msisdn end) as creditlimit_rec,
        from alarms_v2
        where alarmdate = '$today'
        -- fatal error exception :Call to a member function get() on array --
        group by regionalid order by regionalid"))->get();
    

    Here you don't need to call the get() method. Because the select() method already execute the query and return result array. So, just do as bellow:

    $temp = DB::select(DB::raw("select regionalid,
            nvl(sum(case when lastactiontype='0' then totalcharge end),0) as creditlimit_usage, 
            count(case when lastactiontype='0' then msisdn end) as creditlimit_rec,
            from alarms_v2
            where alarmdate = '$today'
            group by regionalid order by regionalid"));