【问题标题】:How to fix "Call to a member function get() on array" : Laravel 5.4如何修复“调用数组上的成员函数 get()”:Laravel 5.4
【发布时间】:2019-04-14 17:57:49
【问题描述】:

我在使用 ma​​atwebsite/laravel-excel 从数据库下载生成数据的文件时遇到问题。错误:调用数组上的成员函数 get()。

以前,当我只使用一个字段从数据库中选择数据时,文件可以生成(.csv)。但是当我尝试使用别名“as”进行查询时,无法获取数据。

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);
}

【问题讨论】:

    标签: php laravel csv report maatwebsite-excel


    【解决方案1】:
    $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();
    

    这里不需要调用get() 方法。因为 select() 方法已经执行了查询并返回了结果数组。因此,只需执行以下操作:

    $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"));
    

    【讨论】:

    • 很高兴听到它对我有用。如果答案是正确的,别忘了点赞并批准答案。
    • 但是,您仍然没有投票并批准答案。
    猜你喜欢
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2017-05-22
    • 2018-06-28
    相关资源
    最近更新 更多