为了账号安全,请及时绑定邮箱和手机立即绑定

如何解决“在数组上调用成员函数get()”:Laravel 5.4

如何解决“在数组上调用成员函数get()”:Laravel 5.4

PHP
Cats萌萌 2021-04-07 17:15:39
使用maatwebsite / 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);}
查看完整描述

1 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

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


查看完整回答
反对 回复 2021-04-23
  • 1 回答
  • 0 关注
  • 152 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信