douzizang7783 2019-04-14 17:57
浏览 95
已采纳

如何修复“在数组上调用成员函数get()”: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);
}
  • 写回答

1条回答 默认 最新

  • duanfen2008 2019-04-14 18:05
    关注
    $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"));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?