0
votes

I created a query that works on a single query but when I use ->get() it prompt

Object of class stdClass could not be converted to string

Here is my code:

  $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');
1

1 Answers

0
votes

Quick solution is decoding it

json_decode( json_encode($data), true);

Here is my code now

$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');