简体   繁体   中英

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.

Now I need to export some data from the DB into an Excel file. So I made an Export class like this:

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:

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:

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:

Return value of Maatwebsite\Excel\Sheet::mapArraybleRow() must be of the type array, null returned

So what is going wrong here? How can I fix this issue and get the data as Excel file properly?

You never execute your $records query, so it is null . Add ->get() to the end of $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() :

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() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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