类stdClass的对象无法转换为字符串-Laravel [英] Object of class stdClass could not be converted to string - laravel
本文介绍了类stdClass的对象无法转换为字符串-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在关注此文档
在我的laravel 4项目中实现导出到Excel.
to implement export to Excel in my laravel 4 project.
因此,我试图从数组中生成excel文件,如下所示:
So am trying to generate excel file from array like this:
//$results is taken with db query
$data = array();
foreach ($results as $result) {
$result->filed1 = 'some modification';
$result->filed2 = 'some modification2';
$data[] = $result;
}
Excel::create('Filename', function($excel) use($data) {
$excel->sheet('Sheetname', function($sheet) use($data) {
$sheet->fromArray($data);
});
})->export('xls');
但这会引发异常:
Object of class stdClass could not be converted to string
我在做什么错了?
更新:
尝试过:
$data = get_object_vars($data);
结果为:
get_object_vars() expects parameter 1 to be object, array given
此:
$data = (array)$data;
导致初始错误.
推荐答案
$data
确实是一个数组,但是它由对象组成.
$data
is indeed an array, but it's made up of objects.
在创建内容之前将其内容转换为数组:
Convert its content to array before creating it:
$data = array();
foreach ($results as $result) {
$result->filed1 = 'some modification';
$result->filed2 = 'some modification2';
$data[] = (array)$result;
#or first convert it and then change its properties using
#an array syntax, it's up to you
}
Excel::create(....
这篇关于类stdClass的对象无法转换为字符串-Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文