【问题标题】:Object of class stdClass could not be converted to string Maatwebsite类 stdClass 的对象无法转换为字符串 Maatwebsite
【发布时间】:2018-10-19 20:07:17
【问题描述】:

我正在尝试导出查询 - 控制器中的功能如下

class Export implements FromCollection
{
    public function collection()
    {
        $data = DB::table('Providers')->get();

        return $data;
    }
}

但产生错误:

供应商\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Cell\DefaultValueBinder.php

“stdClass 类的对象无法转换为字符串”

【问题讨论】:

  • 请显示一些错误的堆栈跟踪。
  • 你好@Camilo 我更新了帖子,谢谢你的评论。

标签: laravel maatwebsite-excel


【解决方案1】:
class Export implements FromCollection
{
    public function collection()
    {
        $data = DB::table('Providers')->get();
        $data = $data->toArray();
        $data = json_decode(json_encode($data), true);
        return $data;
    }
}

【讨论】:

    【解决方案2】:

    您可以尝试从中创建一个集合,然后对其进行映射。

    $dataArray = collect($data)->map( function($stdClass) {
    
        return (array) stdClass;
    }
    

    不确定您的数据是什么样的,也许您也可以将其转换为字符串。 希望这可以帮助。

    【讨论】:

      【解决方案3】:

      你可以这样做

      class Export implements FromCollection
      {
          public function collection()
          {
              $data = DB::table('Providers')->get();
              return $data->toJson();
              return $data->toArray();
          }
      }
      

      【讨论】:

      • 谢谢@Kayle Liu,但我遇到了错误:Call to a member function each() on array in public function fromCollection(FromCollection $sheetExport, Worksheet $worksheet) { $sheetExport ->collection() ->each(function ($row) use ($sheetExport, $worksheet) { $this->appendRow($row, $sheetExport); }); } in vendor\maatwebsite\excel\src\Sheet.php
      • 不确定这应该做什么,但从未调用过第二次返回。即便如此,它仍需要 Collection,因此将其转换为 json 或数组是没有意义的......
      猜你喜欢
      • 2011-04-06
      • 2020-03-16
      • 2021-11-10
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多