类stdClass的对象无法转换为string - laravel(Object of class stdClass could not be converted to string - laravel)
我正在关注这个文档
在我的laravel 4项目中实现导出到Excel。
所以我试图像这样从数组中生成excel文件:
//$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;导致最初的错误。
I am following this documentation
to implement export to Excel in my laravel 4 project.
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');But this raises exception:
Object of class stdClass could not be converted to stringWhat am I doing wrong ?
UPDATE:
Tried this:
$data = get_object_vars($data);which results in:
get_object_vars() expects parameter 1 to be object, array givenThis:
$data = (array)$data;Results in the initial error.
最满意答案
$data实际上是一个数组,但它由对象组成。
在创建它之前将其内容转换为数组:
$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(....$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(....更多推荐
发布评论