bfinlay / laravel-excel-seeder

Seed your database with Laravel using Excel and CSV files
Other
38 stars 8 forks source link

Possible PHP 8 Issue #7

Closed mw7147 closed 3 years ago

mw7147 commented 3 years ago

After upgrading to PHP 8/ Laravel 8 i received the following error while seeding:

SpreadsheetSeeder: File: gosDefault.xlsx Sheet: menuItems Row: 2 24.8 MB 0 s

ValueError

mb_convert_encoding(): Argument #3 ($from_encoding) must specify at least one encoding

at vendor/bfinlay/laravel-excel-seeder/src/SourceRow.php:118 114▕ return $value; 115▕ } 116▕ 117▕ private function encode($value) { ➜ 118▕ if( is_string($value) ) $value = mb_convert_encoding($value, $this->settings->outputEncoding, $this->settings->inputEncodings); 119▕ return $value; 120▕ } 121▕ 122▕ private function hash($columnName, $value) {

  +9 vendor frames 

10 database/seeders/menuData_seeder.php:22 bfinlay\SpreadsheetSeeder\SpreadsheetSeeder::run()

  +7 vendor frames 

18 database/seeders/DatabaseSeeder.php:18 Illuminate\Database\Seeder::call("Database\Seeders\menuData_seeder")

The code was working as expected in PHP 7.4 and crashed immediately upon upgrading my system to PHP 8. It appears the mb_convert_encoding function changed slightly in PHP 8 and crashes when $this->settings->inputEncodings is a empty array. At SourceRow.php line 118 this change corrects the issue (longhand for clarity)

private function encode($value) {
    if( is_string($value) ) {
        if (empty($this->settings->inputEncodings)) {
            $value = mb_convert_encoding($value, $this->settings->outputEncoding);
        } else {
            $value = mb_convert_encoding($value, $this->settings->outputEncoding, $this->settings->inputEncodings);
        }
    }
    return $value;
}

or

in SpreadsheetSeederSettings.php Line 188

public $inputEncodings = null;

Corrects the problem.

bfinlay commented 3 years ago

Thanks! Incorporated your fix and ran tests against php8.