我想生成csv,下面是我的代码。但我得到错误
array_keys() expects parameter 1 to be array, object given
$list = DB::table('festivals')
->join('namedesgs', 'festivals.id', '=', 'namedesgs.festival')
->select(DB::raw("festivals.id ,GROUP_CONCAT(namedesgs.name SEPARATOR '\n') as names,GROUP_CONCAT(namedesgs.designation SEPARATOR '\n') as namese"))
->groupBy('festivals.id')
->get();
array_unshift($list, array_keys($list[0]));
$callback = function () use ($list) {
$FH = fopen('php://output', 'w');
foreach ($list as $row) {
foreach ($row as $key => $val) {
$row[$key] = '="'.str_replace('"', '\"', $val).'"';
}
fputcsv($FH, $row);
}
fclose($FH);
};
return
Response :: stream($ callback,200,$ headers);
答案 0 :(得分:0)
你可以在get()之后使用toArray()函数将结果集合转换为数组,如下所示:
$list = DB::table('festivals')
->join('namedesgs', 'festivals.id', '=', 'namedesgs.festival')
->select(DB::raw("festivals.id ,GROUP_CONCAT(namedesgs.name SEPARATOR '\n') as names,GROUP_CONCAT(namedesgs.designation SEPARATOR '\n') as namese"))
->groupBy('festivals.id')
->get()
->toArray();
然后你的下面代码就可以了!