Return Maatwebsite\Excel\Sheet::mapArraybleRow() 的值必须是数组类型,返回 null
Return value of Maatwebsite\Excel\Sheet::mapArraybleRow() must be of the type array, null returned
我正在使用 Laravel 5.8 和 PHP 7.4 开发我的项目,在这个项目中,我想从 table 中创建一个 Excel 文件称为 students
.
这就是我所做的:
我在终端输入 composer require maatwebsite/excel
并下载了包。
我在 config/app.php
的 providers
中添加了 \Maatwebsite\Excel\ExcelServiceProvider::class,
,并将 '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','mbr_mobile','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)
Return value of Maatwebsite\Excel\Sheet::mapArraybleRow() must be of the type array, null returned
所以这里出了什么问题?
您需要 return 来自 getCustom
方法的数组或对象
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','mbr_mobile','province','city','degree','grade')->get();
return $records;
}
更新您的模态
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','mbr_mobile','province','city','degree','grade')->get();
return $records;
}
在您的导出文件中
return Student::getCustom();
我正在使用 Laravel 5.8 和 PHP 7.4 开发我的项目,在这个项目中,我想从 table 中创建一个 Excel 文件称为 students
.
这就是我所做的:
我在终端输入 composer require maatwebsite/excel
并下载了包。
我在 config/app.php
的 providers
中添加了 \Maatwebsite\Excel\ExcelServiceProvider::class,
,并将 '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','mbr_mobile','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)
Return value of Maatwebsite\Excel\Sheet::mapArraybleRow() must be of the type array, null returned
所以这里出了什么问题?
您需要 return 来自 getCustom
方法的数组或对象
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','mbr_mobile','province','city','degree','grade')->get();
return $records;
}
更新您的模态
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','mbr_mobile','province','city','degree','grade')->get();
return $records;
}
在您的导出文件中
return Student::getCustom();