Laravel Excel News — December ‘21

Patrick Brouwers
Spartner
Published in
3 min readDec 27, 2021

With the end of the year in sight, the 8th Year of Laravel Excel Development comes to an end. We are looking forward to all the great things that 2022 will bring. Here’s one last update for 2021.

PHP 8.1 compatibility

PHP celebrated its 8.1 release recently. That meant some changes would have to be done to make Laravel Excel compatible.

Laravel Excel has a Row object which has some handy methods like $row->toArray(). But you can also use the row as an array: $row['name']. The latter is achieved by using the ArrayAccessinterface.

New typehints

PHP 8.1 has added new typehints to the ArrayAccess interface.

public offsetGet(mixed $offset): mixed

Especially the mixed return type is a tricky one when supporting multiple PHP versions. The mixed return type was only added in PHP 8.0. Adding the return type would throw an error in PHP 7.x and not adding it would throw an error in PHP 8.1.

Laravel Excel currently supports PHP 7.2 till PHP 8, so we couldn’t update the return type without having to deprecate all PHP 7 versions. We currently don’t have plans to do that yet, this is scheduled for the next major release.

The solution

PHP 8.1 also introduced a new #[\ReturnTypeWillChange] attribute. This attribute signals PHP that a mismatching return type should not trigger the deprecation notice and can be added a method.

#[\ReturnTypeWillChange]
public function offsetGet(mixed $offset) {
//
}

The error will now be suppressed in PHP 8.1. However in PHP 9.0 it will trigger an error. The next major release of Laravel Excel (4.0) will stop supporting PHP 7.

Adding PHP 8.1 to our Continuous Integration

Laravel Excel has an extensive test suite that runs on all supported PHP and Laravel versions. PHP 8.1 was added to this list to check if Laravel Excel is fully compatible with PHP 8.1.

Support for column limit and SkipsEmptyRows

The SkipsEmptyRows concern now uses the provided column limit when checking if the row is empty.

class Import implements ToArray, WithColumnLimit, SkipsEmptyRows
{
public function endColumn(): string
{
return 'D';
}
}

Format column now supports ranges

The column formatting concern now also accepts ranges as column coordinates, before it was only possible format the entire column. Now you can apply the formatting to multiple columns or just specific cells.

class Export implements WithColumnFormatting
{
public function columnFormats(): array
{
return [
'A' => NumberFormat::FORMAT_DATE_DDMMYYYY,
'B1:C4' => NumberFormat::FORMAT_CURRENCY_EUR,
];
}
}

Other fixes

  • Change default local_path configuration
  • Fix queueImport function to be able to assert chained jobs
  • Skipped failure no longer persists in ToCollection and ToArray.
  • Fix missing InteractsWithQueue trait in AppendToSheet jobs

Maatwebsite is now Spartner. New name, same dedication to the open source community. Read more at https://spartner.software/maatwebsite-is-now-spartner.

Do you need help with integrating Laravel Excel or do you need help building a Laravel application? We are there to help you on a commercial basis. Contact us via info@spartner.nl or via phone +31 (0)10 744 9312 to discuss the possibilities.

--

--

Patrick Brouwers
Spartner

Loves building awesome things in Laravel, VueJS and TailwindCss. Working @ Spartner. Maintainer of Laravel Excel.