php - 如何修复 “Object of class stdClass could not be converted to string” : Laravel 5.4

标签 php laravel laravel-5 error-handling maatwebsite-excel

我有一个问题,当我尝试使用 maatwebsite/laravel-excel 到.csv从数据库生成数据时,出现如下错误:stdClass类的对象无法转换为字符串。

有时,当我仅使用一个字段从数据库中选择数据时,该文件可以生成(.csv)。这是我的错误:

at HandleExceptions->handleError(4096, 'Object of class stdClass could not be converted to string', '/vendor/phpoffice/phpexcel/Classes/PHPExcel/Cell/DefaultValueBinder.php', 65, array('cell' => object(PHPExcel_Cell), 'value' => object(stdClass)))



这是我从数据库中获取数据的查询,var $ temp用于容纳查询结果。
    public function coba ($type){
        $temp = DB::select(DB::raw("select regionalid, 
        nvl(sum(case when lastactiontype='0' then totalcharge end),0) as creditlimit_usage,
        count(case when lastactiontype='0' then msisdn end) as creditlimit_rec,
        nvl(sum(case when lastactiontype='1' then totalcharge end),0) as blocked_usage,
        count(case when lastactiontype='1' then msisdn end) as blocked_rec,
        nvl(sum(case when lastactiontype='2' then totalcharge end),0) as adjusted_usage,
        count(case when lastactiontype='2' then msisdn end) as adjusted_rec,
        nvl(sum(case when lastactiontype='3' then totalcharge end),0) as sms_usage, 
        count(case when lastactiontype='3' then msisdn end) as sms_rec,
        nvl(sum(case when lastactiontype='5' then totalcharge end),0) as call_usage, 
        count(case when lastactiontype='5' then msisdn end) as call_rec,
        nvl(sum(case when lastactiontype in ('1','2','3','5') then totalcharge end),0) as total_usage, 
        count(case when lastactiontype in ('1','2','3','5') then msisdn end) as total_rec
    from alarms_v2
    where alarmdate = '$today'
    group by regionalid order by regionalid"));

    return Excel::create('datadoc', function ($excel) use ($temp) {
        $excel->sheet('mySheet', function ($sheet) use ($temp) {
            $sheet->fromArray($temp);
        });
    })->download($type);

我希望能够从数据库生成数据到.csv或excel,它可以正常工作。

最佳答案

尝试这个:

$sheet->fromArray((array)$temp);

关于php - 如何修复 “Object of class stdClass could not be converted to string” : Laravel 5.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55681188/

相关文章:

php - jQuery 或 css 来处理选择元素中的长描述

php - 如何在 Laravel 中将连接结果作为对象数组获取?

php - 如何获取字符串中小数点前后的值

php - 理解 php 中的继承

php - Laravel - 根据动态参数扩展 Eloquent where 子句

view - 如何在 Laravel 中渲染 View 并将内容保存在文件中

php - Laravel Log channel 未定义方法

php - Laravel 定义 updated_at 类型的列

php - Laravel 5.6 如何逐行读取文本文件

php - 在 laravel 中同时插入一个有多个关系表