php - stdClass 类的对象无法转换为字符串 - laravel

标签 php excel laravel

我正在关注this documentation

在我的 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;

导致初始错误。

最佳答案

用一行代码尝试这个简单的操作:-

$data= json_decode( json_encode($data), true);

希望有帮助:)

关于php - stdClass 类的对象无法转换为字符串 - laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30260076/

相关文章:

php - oci_error为空

php - 插入 fatal error 函数 bind_param()

php - jQuery - 拖放列表重新排序

excel - 将一列与另一列值相乘

javascript - Gulp cwd 到项目根目录

javascript - 使用 php 更改 css

excel - 是否可以将excel中的命令按钮限制/锁定为每周仅使用一次?

基于单元格值的Excel序列号

php - Laravel 在连接后计算行数

php - Laravel phpunit assertViewHas 与预期的测试数据不匹配