繁体   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

我正在使用 Laravel 8 并且我已经在我的项目中成功安装了Maatwebsite

现在我需要将一些数据从数据库导出到一个 Excel 文件中。 所以我做了一个 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());
    }
}

如您所见,我从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;
    }

然后在Controller,我做了这个方法:

public function exportIntoExcel()
    {
        return Excel::download(new BatchExport,'BatchDownload.xlsx');
    }

这是调用它的路线:

Route::get('/export-excel',[BatchController::class,'exportIntoExcel']);

但是当我转到/export-excel uri 以将数据作为 Excel 文件时,我收到此消息:

Maatwebsite\Excel\Sheet::mapArraybleRow()的返回值必须是数组类型,返回null

那么这里出了什么问题? 如何解决此问题并正确获取数据作为 Excel 文件?

您永远不会执行$records查询,所以它是null 添加->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;
}

或者在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();
}

此外,由于->get()返回一个 Collection,因此您不需要在BatchUpload::getBatch() collect()周围使用 collect() :

public function collection() {
  return BatchUpload::getBatch();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM