【问题标题】:Maatwebsite/Excel Exporting ErrorMaatwebsite/Excel 导出错误
【发布时间】:2018-10-23 21:45:49
【问题描述】:

我正在使用 Laravel 5.4,我想将记录导出到 excel 文件,但出现此错误

传递给 Illuminate\Database\Eloquent\Builder::create() 的参数 1 必须是数组类型,给定字符串,在 C:\xampp\htdocs\www\vendor\laravel\framework\src\Illuminate\ 中调用Database\Eloquent\Model.php 在第 1332 行并已定义

这是我的代码:

   $export = Student::all();
    Excel::create('Export Excel',function($excel) use($export){
        $excel->sheet('Sheet 1', function($sheet) use($export){
            $sheet->fromArray($export);

        });
    })->download('xlsx');
   $export = Student::all();
Excel::create('Export Excel',function($excel) use($export){
    $excel->sheet('Sheet 1', function($sheet) use($export){
        $sheet->fromArray($export);

    });
})->download('xlsx');

【问题讨论】:

  • 第 1332 行代码的内容是什么?

标签: php laravel


【解决方案1】:

$export 是一个 Eloquent 集合,您正在使用 fromArray 方法,因此它需要一个数组。

你可以做两件事:

  1. 将 Eloquent 集合转换为数组:

$sheet->fromArray($export->toArray());

  1. 使用接受 Eloquent 集合的 fromModel 方法:

$sheet->fromModel($export);

【讨论】:

    【解决方案2】:

    也许您有一个名为“Excel”的模型会导致问题。

    导入时只需为“Maatwebsite/Excel”创建一个别名:

    use Maatwebsite\Excel\Facades\Excel as MaatExcel;
    

    【讨论】:

      猜你喜欢
      • 2021-05-01
      • 2019-05-20
      • 1970-01-01
      • 2016-11-23
      • 1970-01-01
      • 2019-05-15
      • 2020-04-03
      • 2020-03-10
      • 1970-01-01
      相关资源
      最近更新 更多