[英]Getting "Return value of Maatwebsite\Excel\Sheet::mapArraybleRow() must be of the type array, null returned" error while exporting data
I'm using Laravel 8 and I have successfully installed Maatwebsite on my project.我正在使用 Laravel 8 并且我已经在我的项目中成功安装了Maatwebsite 。
Now I need to export some data from the DB into an Excel file.现在我需要将一些数据从数据库导出到一个 Excel 文件中。 So I made an Export class like this:
所以我做了一个 Export class 这样的:
class BatchExport implements FromCollection
{
public function headings():array{
return [
'Id',
'product_group',
'title_product_variety',
'product_code',
'variety_code',
'seller_code',
'activation',
'activation',
'send_interval',
'sell_price',
'mortal_price',
'promotion_price',
'consumable_balance',
'reserve',
'seller_inventory',
'digikala_inventory',
'maximum_order_count'
]
}
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return collect(BatchUpload::getBatch());
}
}
So as you can see I have called the getBatch
method from BatchUpload
Model which is this:如您所见,我从
BatchUpload
Model 调用了getBatch
方法,它是这样的:
public static function getBatch()
{
$records = DB::table('batch_upload')->select('bid','product_group','title_product_variety','product_code','variety_code','variety_code','seller_code','activation','send_interval','sell_price','mortal_price','promotion_price','consumable_balance','reserve','seller_inventory','digikala_inventory','maximum_order_count');
return $records;
}
Then at the Controller, I made this method:然后在Controller,我做了这个方法:
public function exportIntoExcel()
{
return Excel::download(new BatchExport,'BatchDownload.xlsx');
}
And this is the route that calls it:这是调用它的路线:
Route::get('/export-excel',[BatchController::class,'exportIntoExcel']);
But when I goto /export-excel
uri to get the data as Excel file, I get this message:但是当我转到
/export-excel
uri 以将数据作为 Excel 文件时,我收到此消息:
So what is going wrong here?那么这里出了什么问题? How can I fix this issue and get the data as Excel file properly?
如何解决此问题并正确获取数据作为 Excel 文件?
You never execute your $records
query, so it is null
.您永远不会执行
$records
查询,所以它是null
。 Add ->get()
to the end of $records = DB::table(...)->select()->get()
:添加
->get()
到$records = DB::table(...)->select()->get()
的末尾:
public static function getBatch() {
$records = DB::table('batch_upload')->select('bid','product_group','title_product_variety','product_code','variety_code','variety_code','seller_code','activation','send_interval','sell_price','mortal_price','promotion_price','consumable_balance','reserve','seller_inventory','digikala_inventory','maximum_order_count')->get();
return $records;
}
Or tack it on before return
, like return $records->get()
:或者在
return
之前添加它,比如return $records->get()
:
public static function getBatch() {
$records = DB::table('batch_upload')->select('bid','product_group','title_product_variety','product_code','variety_code','variety_code','seller_code','activation','send_interval','sell_price','mortal_price','promotion_price','consumable_balance','reserve','seller_inventory','digikala_inventory','maximum_order_count');
return $records->get();
}
Also since ->get()
returns a Collection, so you wouldn't need collect()
around BatchUpload::getBatch()
:此外,由于
->get()
返回一个 Collection,因此您不需要在BatchUpload::getBatch()
collect()
周围使用 collect() :
public function collection() {
return BatchUpload::getBatch();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.