如何解决Maatwebsite\Excel\Sheet::mapArraybleRow() 的返回值必须是数组类型,返回null
我正在使用 Laravel 5.8 和 PHP 7.4 来开发我的项目,在这个项目中,我想从一个名为 students
的表中创建一个 Excel 文件。
这就是我所做的:
我在终端输入 composer require maatwebsite/excel
并下载了软件包。
我在 \Maatwebsite\Excel\ExcelServiceProvider::class,
的 providers
中添加了 config/app.php
,并将 'Excel' => \Maatwebsite\Excel\Facades\Excel::class,
添加为 aliases
。
然后我将此方法添加到模型 Student.php
中:
public static function getCustom()
{
$records = DB::table('students')->select('mbr_id','mbr_mobile','mbr_post_code','mbr_address','mbr_name','mbr_family','mbr_national_code','province','city','degree','grade');
return $records;
}
然后制作了这个新的导出文件:
php artisan make:export StudentExport --model=App\Member\Student
这个文件保存了这个:
use App\Member\Student;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\withHeadings;
class StudentExport implements FromCollection,withHeadings
{
public function headings(): array
{
return [
'Name','Family Name','National Code','Mobile Number','Province','City','Degree'
'Grade','Registered at',];
}
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return collect(Student::getCustom());
}
}
在控制器中,我添加了以下方法:
public function exportIntoExcel()
{
return Excel::download(new StudentExport,'studentlist.xlsx');
}
public function exportIntoCSV()
{
return Excel::download(new StudentExport,'studentlist.csv');
}
还有这些到web.php
的路线:
Route::get('export-excel','StudentAdminController@exportIntoExcel');
Route::get('export-csv','StudentAdminController@exportIntoCSV');
但是当我尝试这两条路线时,结果如下:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR)
Maatwebsite\Excel\Sheet::mapArraybleRow()的返回值必须是数组类型,返回null
所以这里出了什么问题?
解决方法
更新你的模态
public static function getCustom()
{
$records = DB::table('students')->select('mbr_id','mbr_mobile','mbr_post_code','mbr_address','mbr_name','mbr_family','mbr_national_code','province','city','degree','grade')->get();
return $records;
}
在您的导出文件中
return Student::getCustom();
,
您需要从 getCustom
方法返回一个数组或对象
public static function getCustom()
{
$records = DB::table('students')->select('mbr_id','grade')->get();
return $records;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。