如何解决类 stdClass 的 Laravel Excel 对象无法转换为字符串
我创建了一个适用于单个查询的查询,但是当我使用 ->get()
时它会提示
Object of class stdClass could not be converted to string
这是我的代码:
$result = DB::table('users')->get(); // the data you want to download as csv
$csv = (array)$result; // stored the data in a array
return Excel::create('csvfile',function ($excel) use ($csv) {
$excel->sheet('mySheet',function ($sheet) use ($csv) {
$sheet->fromArray($csv);
});
})->download('xls');
解决方法
快速解决方案是解码
json_decode( json_encode($data),true);
这是我的代码
$data = DB::table('users')->get();
$csv= json_decode( json_encode($data),true);
return Excel::create('SalesReport',function($excel) use ($csv) {
$excel->sheet('Sales-Report',function($sheet) use ($csv)
{
$sheet->fromArray($csv);
});
})->download('xls');
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。