簡體   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