简体   繁体   English

导出数据时出现“Maatwebsite\Excel\Sheet::mapArraybleRow() 的返回值必须是数组类型,返回 null”错误

[英]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 文件时,我收到此消息:

Return value of Maatwebsite\Excel\Sheet::mapArraybleRow() must be of the type array, null returned Maatwebsite\Excel\Sheet::mapArraybleRow()的返回值必须是数组类型,返回null

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.

相关问题 Maatwebsite\\Excel\\Sheet::mapArraybleRow() 的错误返回值必须是数组类型,返回字符串 - Errorr Returnn value of Maatwebsite\Excel\Sheet::mapArraybleRow() must be of the type array, string returned 致命错误:未捕获的 TypeError:safeQuery() 的返回值必须是数组或 null 类型,返回 bool - Fatal error: Uncaught TypeError: Return value of safeQuery() must be of the type array or null, bool returned Maatwebsite / Excel导出错误 - Maatwebsite/Excel Exporting Error 尝试计算导入的 excel 文件的行数时出现错误“返回值必须是 int 类型,返回数组” - I got an error “Return value must be of the type int, array returned” when trying to count rows of imported excel file Myrepository的返回值必须是MyEntity的实例或为null,返回数组 - Return value of Myrepository must be an instance of MyEntity or null, array returned Magento 2 错误:Magento\Framework\Filter\DirectiveProcessor\TemplateDirective::process() 的返回值必须是字符串类型,返回null - Magento 2 Error: Return value of Magento\Framework\Filter\DirectiveProcessor\TemplateDirective::process() must be of the type string, null returned carsAction() 的返回值必须是数组类型,返回 object - Return value of carsAction() must be of the type array, object returned 将数据库导出到 excel 时出现错误“数据类型 numeric in…的数值无效” - Error “invalid numeric value for data type numeric in…” while exporting database to excel 上传 Excel 时在 Laravel (Maatwebsite\Excel\Facades\Excel) 中显示未定义的数组键“Sheet 1” - While uploading Excel Showing Undefined array key “Sheet 1” in Laravel (Maatwebsite\Excel\Facades\Excel) 使用Laravel Maatwebsite导出到Excel时丢失数据 - Missing data when exporting to excel using Laravel Maatwebsite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM