Laravel-excel: "count(): Parameter must be an array or an object that implements Countable"

Created on 21 Dec 2017  路  12Comments  路  Source: Maatwebsite/Laravel-Excel

Happening on both PHP7.1 and PHP7.2.

[2017-12-21 22:34:33] acceptance.INFO: ErrorException: count(): Parameter must be an array or an object that implements Countable in /home/forge/website.com/releases/20171221213319/vendor/maatwebsite/excel/src/Maatwebsite/Excel/Classes/LaravelExcelWorksheet.php:599
Stack trace:
#0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'count(): Parame...', '/home/forge/acc...', 599, Array)
#1 /home/forge/website.com/releases/20171221213319/vendor/maatwebsite/excel/src/Maatwebsite/Excel/Classes/LaravelExcelWorksheet.php(599): count(92)
#2 /home/forge/website.com/releases/20171221213319/vendor/maatwebsite/excel/src/Maatwebsite/Excel/Classes/LaravelExcelWorksheet.php(556): Maatwebsite\Excel\Classes\LaravelExcelWorksheet->addData(Array)
#3 /home/forge/website.com/releases/20171221213319/vendor/maatwebsite/excel/src/Maatwebsite/Excel/Classes/LaravelExcelWorksheet.php(478): Maatwebsite\Excel\Classes\LaravelExcelWorksheet->_addVars(Object(Illuminate\Support\Collection), false, NULL, 'A1', false)

Most helpful comment

...talking about ugly workarounds: I "fixed" this by adding the following to composer.json:

"post-install-cmd": [
    "Illuminate\\Foundation\\ComposerScripts::postInstall",
    "sed -i '768s/.*/if(!empty($columns)){/' vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/Excel2007/Worksheet.php"
],
"post-update-cmd": [
    "Illuminate\\Foundation\\ComposerScripts::postUpdate",
    "sed -i '768s/.*/if(!empty($columns)){/' vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/Excel2007/Worksheet.php"
]

It just replaces the faulty line after each composer update or composer install.

All 12 comments

Which version are you using @Jaspur ? Seems like count function is called on non-countable variable. Looks like an easy fix.

Have run into this issue also - running on Laravel 5.5 & PHP 7.2.

I have updated the package to the latest version (2.1.24) which I believe was intended to include a fix for this issue, but apparently it still persists.

Just a bit of further info on this - it's triggered when I run $export->download('xlsx'); but csv works fine.

Okay so from further digging the error I'm encountering actually comes from phpoffice/phpexcel:

/vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/Excel2007/Worksheet.php:768

On this line they do:
if (count($columns > 0))

Which is obviously where the route of the issue comes from, if I just swap this to do a simple !empty then it goes through fine.

Update: it only occurs when using ->setAutoFilter()

I can confirm the issue with count(NULL) @andyunleashed mentioned. Since it's happening within an unsupported repository I don't see a clean solution. I used an ugly temporary fix , but there has to be a better way.

Fix in this package was released, fixes needing to happen in PHPExcel, we can't do anything about.

Since PHPExcel was last updated in 2015 I don't see that happening any time soon. Is there anything on the roadmap to move to PHPSpreadsheet which appears to be the successor?

Soonish, but it won鈥檛 be backwards compatible.

...talking about ugly workarounds: I "fixed" this by adding the following to composer.json:

"post-install-cmd": [
    "Illuminate\\Foundation\\ComposerScripts::postInstall",
    "sed -i '768s/.*/if(!empty($columns)){/' vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/Excel2007/Worksheet.php"
],
"post-update-cmd": [
    "Illuminate\\Foundation\\ComposerScripts::postUpdate",
    "sed -i '768s/.*/if(!empty($columns)){/' vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/Excel2007/Worksheet.php"
]

It just replaces the faulty line after each composer update or composer install.

@S43534 I dunno what you just did, but I "fixed" it too, whats the side effect ?

I think more proper way of fixing this would be:

  1. fork this repository and fix bug in code
  2. change composer.json inf forked project so it will have updated project name
    "name": "fork-account/phpexcel",
  3. push changes to forked project
  4. update your project composer.json require section:
    "fork-account/phpexcel": "dev-master#LATEST_COMMIT_HASH_WITH_FIX"
  5. add repositories to composer.json
    "repositories": [ { "type": "vcs", "url": "https://github.com/fork-account/phpexcel.git" } ],
Was this page helpful?
0 / 5 - 0 ratings

Related issues

amine8ghandi8amine picture amine8ghandi8amine  路  3Comments

rossjcooper picture rossjcooper  路  3Comments

gamevnlc picture gamevnlc  路  3Comments

octoxan picture octoxan  路  3Comments

muhghazaliakbar picture muhghazaliakbar  路  3Comments