类stdClass的对象无法转换为string

编程入门 行业动态 更新时间:2024-10-26 20:26:27
类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 string

What 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 given

This:

$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(....

更多推荐

本文发布于:2023-07-27 15:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292435.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   对象   stdClass   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!