How to design excel sheet using maatwebsite in lar

2019-07-14 19:49发布

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条回答
放荡不羁爱自由
2楼-- · 2019-07-14 20:36

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

查看更多
登录 后发表回答