Laravel-Excel: [BUG] Import EXCEL happens exception at PHP7.4, response: Trying to access array offset on value of type int

Versions

  • PHP version: PHP7.4 (Centos7 yum installed php74 form remisrepo stable version)
  • Laravel version: Laravel Framework 5.8.35
  • Package version: maatwebsite/excel 3.1.17

Description

Import EXCEL happens exception at PHP7.4, Exception->message() is: Trying to access array offset on value of type int.

Steps to Reproduce

Expected behavior:

import a xlsx file as i running the code on php72w

Actual behavior: happened after I uploaded my server circumstance from php7.2 to php7.4 when I import a Excel would be fail like above.

would be normal again if I downgrade from php7.4 to php7.2.

Additional Information

$extension=$request->file()['file']->getClientOriginalExtension();
$extension[0] = strtoupper($extension[0]);
Excel::import(new RejectedImport(), $request->file()['file']->path(),null,$extension);

And what is in Import Class:

<?php

namespace App\Imports;

use App\Commodity;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class CommodityImport implements ToCollection, WithHeadingRow
{
    protected $isOverride=false;
    public function __construct($isOverride)
    {
        if($isOverride=='1')
            $this->isOverride=true;
    }

    /**
     * @param Collection $collections
     */
    public function collection(Collection $collections)
    {
        foreach ($collections as $row)
        {
            $barcode = $row['barcode'] ?? $row['BARCODE'] ?? $row['Barcode'];
            if(!$barcode)continue;
            $name = $row['name'] ?? $row['NAME'] ?? $row['Name'] ?? '';
            $sku = $row['sku'] ?? $row['SKU'] ?? $row['Sku'] ?? '';
            $owner = $row['owner'] ?? $row['owner_name'] ?? $row['OWNER'] ?? $row['Owner'] ?? '';
            $commodity=Commodity::where('barcode',$row['barcode'])->first();
            if($commodity){
                if($this->isOverride){
                    $name?$commodity['name']= $name:false;
                    $sku?$commodity['sku']= $sku:false;
                    $owner?$commodity['owner_name']= $owner:false;
                    $commodity->update();
                }
            }else{
                Commodity::create([
                    'name' => $name,
                    'sku' => $sku,
                    'owner_name' => $owner,
                    'barcode' => $barcode,
                ]);
            }
        }
    }
}

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 24 (5 by maintainers)

Commits related to this issue

Most upvoted comments

In the PHPExcel file “DefaultValueBinder.php”, replace this line 82: } elseif ($pValue[0] === ‘=’ && strlen($pValue) > 1) { with the following: } elseif (0 === strpos($pValue, ‘=’) && strlen($pValue) > 1) { This will fix the “Trying to access array offset on value of type int” error. But you will also need to conduct a find and replace for curly braces throughout the PHPExcel code to address the “Array and string offset access syntax with curly braces is deprecated” error which also affects PHP ugrades to 7.4. I simply replaced them with “[” and “]” everywhere and everything is working fine again.

The best thing to do is to update the PHPOffice/PhpSpreadsheet above 1.10 in your project - because then this Error will be fixed without having to manually edit the package codebase. So for me

composer require phpoffice/phpspreadsheet "^1.10"

fixed the problem.

} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {

strpos of bool, rly?

Just put this code after check to is_bool and is_float

Please report errors within PhpSpreadsheet to PhpSpreadsheet.

DefaultValueBinder.php inside of vendor folder? or where is it? same problem here.

Hi @patrickbrouwers thanks for the package, I’m a big fan of it!

I am getting a similar error after upgrading, although it is during an export, and the stacktrace indicates the problem is with phpsreadsheet, not Laravel-Excel, but google brought me here.

Here is the stacktrace:

#0 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/DefaultValueBinder.php(56): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError()
#1 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/DefaultValueBinder.php(34): PhpOffice\\PhpSpreadsheet\\Cell\\DefaultValueBinder::dataTypeForValue()
#2 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Cell.php(184): PhpOffice\\PhpSpreadsheet\\Cell\\DefaultValueBinder->bindValue()
#3 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Worksheet/Worksheet.php(2510): PhpOffice\\PhpSpreadsheet\\Cell\\Cell->setValue()
#4 /vendor/maatwebsite/excel/src/Sheet.php(402): PhpOffice\\PhpSpreadsheet\\Worksheet\\Worksheet->fromArray()
#5 /vendor/maatwebsite/excel/src/Sheet.php(502): Maatwebsite\\Excel\\Sheet->append()
#6 /vendor/maatwebsite/excel/src/Sheet.php(368): Maatwebsite\\Excel\\Sheet->appendRows()
#7 /vendor/maatwebsite/excel/src/Sheet.php(195): Maatwebsite\\Excel\\Sheet->fromCollection()
#8 /vendor/maatwebsite/excel/src/Writer.php(73): Maatwebsite\\Excel\\Sheet->export()
#9 /vendor/maatwebsite/excel/src/Excel.php(176): Maatwebsite\\Excel\\Writer->export()
#10 /vendor/maatwebsite/excel/src/Excel.php(97): Maatwebsite\\Excel\\Excel->export()
#11 /app/Console/Commands/EmailCardRequestList.php(89): Maatwebsite\\Excel\\Excel->store()