【问题标题】:How to fix "Object of class stdClass could not be converted to string" : Laravel 5.4如何修复“stdClass 类的对象无法转换为字符串”:Laravel 5.4
【发布时间】:2019-04-15 00:17:27
【问题描述】:

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

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

在 HandleExceptions->handleError(4096, '类 stdClass 的对象可以 不转换为字符串', '/vendor/phpoffice/phpexcel/Classes/PHPExcel/Cell/DefaultValueBinder.php', 65, 数组('cell' => object(PHPExcel_Cell), 'value' => 对象(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 它可以正常工作。

【问题讨论】:

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


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
    猜你喜欢
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多