如何在Laravel中使用maatwebsite设计Excel工作表 [英] How to design excel sheet using maatwebsite in laravel

查看:89
本文介绍了如何在Laravel中使用maatwebsite设计Excel工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用 laravel 中的 maatwebsite 包导出了excel CSV文件.但是,当我导出它时,它会显示原始数据,如何设计此excel以便分别显示每个数据及其标题.

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.

这是我导出的数据图像链接. http://prntscr.com/i08up8 这是excel导出代码.

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

推荐答案

问题是以下几行:

 $items=array($items);

Laravel返回一个集合.因此,这意味着您将整个集合放在一个数组中.

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

尝试以下方法:

$items = $items->toArray(); 

这会将您的收藏集转换为数组,并应该解决您的问题

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

这篇关于如何在Laravel中使用maatwebsite设计Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆