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 · 3 comments
Open
1 task done

[Bug]: Laravel 12 - Incompatible #4289

eliassr75 opened this issue Apr 2, 2025 · 3 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;
     }

 	}
}

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

4 participants