php - 如何修复“调用数组上的成员函数 get()”:Laravel 5.4
问题描述
我无法使用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);
}
解决方案
$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"));
推荐阅读
- groovy - Groovy 中缺少 HttpBuilder 的类
- c# - (MVC)尝试通过 LinQ 自己加入一个表以显示多种语言
- webpack - 安装 SimpleUploadAdapter 时的 ckeditor-duplicated-modules
- css - 如何使用作用域css
- java - Java 中 Android 的 Base64.encodeToString(data, Base64.NO_WRAP) 的等价物是什么
- python - 图片按钮 Kivy
- google-bigquery - 没有 BigQuery 的大量数据的交集
- python - 从日期时间选择时间
- ios - 使用 Unity3D 加载场景时,iOS 在 iPhone6 上构建崩溃,最新更新 12.4.3 - Google Cardboard
- javascript - 如何避免在 axios.get 中使用 Web 上下文