How to design excel sheet using maatwebsite in laravel

橙三吉。 提交于 2020-01-04 23:06:38

问题


I have exported excel CSV file using maatwebsite package in laravel. But when I export it, it's show the raw data, how can I design this excel for show every data separately with it's title.

Here, is my exported data image link. http://prntscr.com/i08up8 Here is excel export code.

 public function export()
{
  $items = DB::table('userinformations')
        ->join('users', 'userinformations.user_id', '=', 'users.id')
        ->select('userinformations.fname','userinformations.lname','users.email')
        ->where('userinformations.payment_status',1)
        ->get();
    $items=array($items);
  Excel::create('items', function($excel) use($items) {
      $excel->sheet('ExportFile', function($sheet) use($items) {
          $sheet->fromArray($items);
      });
  })->export('csv');
}

回答1:


The problem is the following line:

 $items=array($items);

Laravel returns a collection. So this would mean you put the entire collection in an (one) array.

Try this instead:

$items = $items->toArray(); 

This will cast your collection to an array and should solve your issue



来源:https://stackoverflow.com/questions/48250034/how-to-design-excel-sheet-using-maatwebsite-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!