【问题标题】:Laravel Excel Object of class stdClass could not be converted to string类 stdClass 的 Laravel Excel 对象无法转换为字符串
【发布时间】:2021-05-28 16:36:29
【问题描述】:

我创建了一个适用于单个查询的查询,但是当我使用 ->get() 时它会提示

Object of class stdClass could not be converted to string

这是我的代码:

  $result = DB::table('users')->get();  // the data you want to download as csv

  $csv = (array)$result;  // stored the data in a array

  return Excel::create('csvfile', function ($excel) use ($csv) {
      $excel->sheet('mySheet', function ($sheet) use ($csv) {
          $sheet->fromArray($csv);
      });
  })->download('xls');

【问题讨论】:

    标签: excel laravel maatwebsite-excel


    【解决方案1】:

    快速解决方案是对其进行解码

    json_decode( json_encode($data), true);
    

    这是我的代码

    $data = DB::table('users')->get();
    $csv= json_decode( json_encode($data), true);
    
    return Excel::create('SalesReport', function($excel) use ($csv) {
        $excel->sheet('Sales-Report', function($sheet) use ($csv)
        {
            $sheet->fromArray($csv);
        });
    })->download('xls');
    

    【讨论】:

      猜你喜欢
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      相关资源
      最近更新 更多