php - 如何在 laravel 中使用 maatwebsite 设计 excel 表格

标签 php laravel maatwebsite-excel

我在 laravel 中使用 maatwebsite 包导出了 excel CSV 文件。但是当我导出它时,它显示的是原始数据,我该如何设计这个 excel 来分别显示每个数据及其标题。

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

 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 返回一个集合。所以这意味着你将整个集合放在一个(一个)数组中。

试试这个:

$items = $items->toArray(); 

这会将您的集合转换为一个数组,应该可以解决您的问题

关于php - 如何在 laravel 中使用 maatwebsite 设计 excel 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48250034/

相关文章:

php - 将表 1 数据添加到表 2 下拉列表中

php - SQL选择查询

windows - Maatwebsite 导入在 ubuntu 中不起作用

html - 使用 VueJs 添加动态输入字段

javascript - 提交后如何从下拉列表中删除选定的值

Php split/chunk Excel with Maatwebsite Laravel-Excel v.3

php - Laravel maatwebsite/excel composer 在 mac 中安装错误

php - 非常担心 Magento 的性能

php - 使用php将html文本转换为图像

php - 调用未定义的函数 Project\Http\Controllers\array_sort()