Skip to content

[Bug]: Laravel 12 - Incompatible #4289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
eliassr75 opened this issue Apr 2, 2025 · 11 comments
Open
1 task done

[Bug]: Laravel 12 - Incompatible #4289

eliassr75 opened this issue Apr 2, 2025 · 11 comments
Labels

Comments

@eliassr75
Copy link

Is the bug applicable and reproducable to the latest version of the package and hasn't it been reported before?

  • Yes, it's still reproducable

What version of Laravel Excel are you using?

3.1

What version of Laravel are you using?

12

What version of PHP are you using?

8.3

Describe your issue

Illuminate\Foundation\ComposerScripts::postAutoloadDump
@php artisan package:discover --ansi

PHP Fatal error: Declaration of Maatwebsite\Excel\Cache\MemoryCache::get(string $key, mixed $default = null): mixed must be compatible with PsrExt\SimpleCache\CacheInterface::get($key, $default = ) in /home/---/dev/gitlab/stock-control/vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 63
PHP Fatal error: Declaration of Monolog\Logger::emergency(Stringable|string $message, array $context = []): void must be compatible with PsrExt\Log\LoggerInterface::emergency($message, array $context = []) in /home/---/dev/gitlab/stock-control/vendor/monolog/monolog/src/Monolog/Logger.php on line 683
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 25

How can the issue be reproduced?

composer require maatwebsite/excel:^3.1

What should be the expected behaviour?

--

@eliassr75 eliassr75 added the bug label Apr 2, 2025
@MdAnowarHosen
Copy link

composer require "maatwebsite/excel:^3.1" that command does not works. but composer require "maatwebsite/excel works

@Tannenfels
Copy link

composer require "maatwebsite/excel:^3.1" that command does not works. but composer require "maatwebsite/excel works

That will install obsolete 1.1, though

@JoelFarell
Copy link

II have had the same problem. Apparently it has to do with the chunking that eloquent does when querying the DB. The only solution I have found is to do it in a more rudimentary way with the PhpSpreadSheet package. Attached below is an example similar to the one I used:

<?php


namespace App\Traits;


use App\Models\Property;
use App\Models\Request;
use App\Models\Supply;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Facades\Log;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;


trait ExportFile
{
 private function pdf($query, $filename)
 {
     // Detectar columnas automáticamente
     $columns = $query->first() ? array_keys($query->first()->getAttributes()) : [];


     // Construir el HTML manualmente
     $html = '<table border="1" cellpadding="0" cellspacing="0" width="100%"><thead><tr>';
     foreach ($columns as $column) {
     	$html .= '<th>' . ucfirst($column) . '</th>';
     }
     $html .= '</tr></thead><tbody>';


     $page = 1;
     $perPage = 1000;
     do {
             $items = $query->forPage($page, $perPage)->get();
             foreach ($items as $item) {
                 $html .= '<tr>';
                 foreach ($columns as $column) {
                     $html .= '<td>' . e($item->$column) . '</td>';
             }
            $html .= '</tr>';
            }
     	$page  ;
     } while ($items->count() > 0);


     $html .= '</tbody></table>';


     // Generar el PDF
     $pdf = Pdf::loadHTML($html)->setPaper('a4', 'landscape');


     // Obtener el contenido binario del PDF
     $pdfContent = $pdf->output();


     // Guardar en storage local
     $storagePath = 'exports/' . $filename . '_' . date('Ymd_His') . '.pdf';
     Storage::disk('local')->put($storagePath, $pdfContent);


     return $storagePath; // Ruta relativa en storage/app/pdf/
 }


 private function csv($data, $filename)
 {
     // 1. Detectar columnas automáticamente
     $query = $data;
     $columns = $data->first() ? array_keys($data->first()->getAttributes()) : [];


     // 2. Crear un recurso temporal
     $temp = fopen('php://temp', 'r ');


     // 3. Escribir encabezados
     fputcsv($temp, $columns);


     // 4. Escribir los datos en chunks para eficiencia
     $page = 1;
     $perPage = 1000;
     do {
     $items = $query->forPage($page, $perPage)->get();

         foreach ($items as $item) {
             $row = [];
             foreach ($columns as $column) {
                 $row[] = $item->$column;
             }
             fputcsv($temp, $row);
         }
	     $page  ;
     } while ($items->count() > 0);


     // 5. Obtener el contenido y guardarlo en storage
     rewind($temp);
     $csvContent = stream_get_contents($temp);
     fclose($temp);


     // 6. Guardar usando Storage
     $storagePath = 'exports/' . $filename . '_' . date('Ymd_His') . '.csv';
     Storage::disk('local')->put($storagePath, $csvContent);


     return $storagePath;
 }


 private function xls($query, $filename, $file_type = 'xlsx') 
 {
     $spreadsheet = new Spreadsheet();
     $sheet = $spreadsheet->getActiveSheet();
     $sheet->setTitle($filename);
     $columns = $query->first() ? array_keys($query->first()->getAttributes()) : [];


         foreach ($columns as $colIndex => $column) {
             $cellCoordinate = Coordinate::stringFromColumnIndex($colIndex   1) . '1';
             $sheet->setCellValue($cellCoordinate, ucfirst($column));
         }


     $page = 1;
     $perPage = 1000;
     $rowIndex = 2;
     do {
         $items = $query->forPage($page, $perPage)->get();
         foreach ($items as $item) {
             foreach ($columns as $colIndex => $column) {
                 $cellCoordinate = Coordinate::stringFromColumnIndex($colIndex   1) . $rowIndex;
                 $sheet->setCellValue($cellCoordinate, $item->$column);
             }
         	$rowIndex  ;
         }
	     $page  ;
     } while ($items->count() > 0);

     // Guardar en un recurso temporal
     $tempFile = tmpfile();
     $tempFilePath = stream_get_meta_data($tempFile)['uri'];

     $writer = new Xlsx($spreadsheet);
     $writer->save($tempFilePath);

     // Leer el contenido binario
     $excelContent = file_get_contents($tempFilePath);

     // Guardar usando Storage en el disco local
     $storagePath = 'exports/' . $filename . '_' . date('Ymd_His') .'.'.$file_type;
     Storage::disk('local')->put($storagePath, $excelContent);

     // Cerrar el recurso temporal
     fclose($tempFile);
     return $storagePath; // Devuelve la ruta relativa dentro de storage/app
 }

 private function get_data(){
   return Data::where('id','>',0); 
 }
 
     public function export($exports='data', $fileName, $file_type)
     {

     try {

     $data = [];
     switch ($exports) {
       case 'data':
          $data = $this->get_data();
       break;
       }
     switch ($file_type) {
        case 'pdf':
            $path = $this->pdf($data, $fileName);
         break;
        case 'xlsx':
            $path = $this->xls($data, $fileName, 'xlsx');
        break;
         case 'xls':
            $path = $this->xls($data, $fileName,'xls');
         break;
         case 'csv':
            $path = $this->csv($data, $fileName);
         break;
     }


     return $path;


     } catch (\Exception $e) {
     Log::error(['[EXPORT ERROR]',$e->getMessage()]);
     return false;
     }

 	}
}

@cosecantt
Copy link

composer require "maatwebsite/excel:^3.1"
did not work with me also.

I use followings:

Laravel 12.10.2
PHP 8.3
Composer 2.8.8

works. but composer require "maatwebsite/excel works

It installs 1.1 version.

  - Downloading maatwebsite/excel (v1.1.5)
  - Installing phpoffice/phpexcel (1.8.1): Extracting archive
  - Installing maatwebsite/excel (v1.1.5): Extracting archive```

@patrickbrouwers
Copy link
Member

Please share the composer output when installing 3.1

@cosecantt
Copy link

Please share the composer output when installing 3.1

Laravel 12.11.1
PHP 8.3
Composer 2.8.8

Laravel Excel installation based on the instruction. The results are:

PS D:\Code\Herd\laravel> composer require "maatwebsite/excel:^3.1"
The "3.1" constraint for "maatwebsite/excel" appears too strict and will likely not match what you want. See https://getcomposer.org/constraints
./composer.json has been updated
Running composer update maatwebsite/excel
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires maatwebsite/excel 3.1 -> satisfiable by maatwebsite/excel[3.1.0].
    - maatwebsite/excel 3.1.0 requires php ^7.0 -> your php version (8.3.20) does not satisfy that requirement.


Installation failed, reverting ./composer.json and ./composer.lock to their original content.
PS D:\Code\Herd\laravel>

@YIPZIYAN
Copy link

YIPZIYAN commented May 7, 2025

composer require "maatwebsite/excel:^3.1" that command does not works. but composer require "maatwebsite/excel works

That will install obsolete 1.1, though

I was able to solve this by installing simple-cache first
composer require psr/simple-cache ^2.0

Then install maatwebsiteexcel
composer require maatwebsite/excel

Ensure that extension=gd is enabled

Source: https://laracasts.com/discuss/channels/laravel/error-to-install-maatwebsiteexcel

@far1023
Copy link

far1023 commented May 7, 2025

I was able to solve this by installing simple-cache first composer require psr/simple-cache ^2.0

Then install maatwebsiteexcel composer require maatwebsite/excel

Ensure that extension=gd is enabled

Source: https://laracasts.com/discuss/channels/laravel/error-to-install-maatwebsiteexcel

Ive tried this but since the psr is included in laravel it doesnt have any impact?

For me, installing ^3.1 will cause error on php version. It ask for php7. Tried the other way to install composer require maatwebsite/excel got an error like this #4280

@YIPZIYAN
Copy link

YIPZIYAN commented May 8, 2025

Have you install composer require phpoffice/phpspreadsheet ?

I did install it first before installing composer require maatwebsite/excel

I think ^3.1 will install phpspreadsheet automatically, but for composer require maatwebsite/excel you need to manually install phpspreadsheet.

Your maatwebsite/excel might be 1.x which is unsupported causing that error happen

@far1023
Copy link

far1023 commented May 8, 2025

Have you install composer require phpoffice/phpspreadsheet ?

I did install it first before installing composer require maatwebsite/excel

I think ^3.1 will install phpspreadsheet automatically, but for composer require maatwebsite/excel you need to manually install phpspreadsheet.

Your maatwebsite/excel might be 1.x which is unsupported causing that error happen

Ya, i have installed phpoffice/phpspreadsheet turned out it was required by yajra/laravel-datatables in my project and locked to ^4.0.
I manage to install the latest version by composer require maatwebsite/excel:"^3.1.64" phpoffice/phpspreadsheet:"^1.29.9" --update-with-dependencies, need to downgrade installed phpspreadsheet. Lucky me Yajra also satisfied by that and v3.1.64 dont give php version error.

@vozaldi
Copy link

vozaldi commented May 20, 2025

composer require maatwebsite/excel:"^3.1.64" phpoffice/phpspreadsheet:"^1.29.9" --update-with-dependencies

This one works for me. Since I already installed the latest phpoffice/phpspreadsheet which may caused a conflict. Thanks @far1023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants