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

如何修复“在数组上调用成员函数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"));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料