0
    public function export(Request $request)
        {
            try {
    
         $query= MyModel::where('uuid', $request->uuid)->with('locations')->first();
            
  //format name
  $name = $query->name;
  $name = strtolower($name); //According to the message returned by the catch it indicates that it is in this line where it fails 
  $name = str_replace(' ', '-', $name);
  $name = preg_replace('/[^A-Za-z0-9\-]/', '', $name);      
  $fileName = 'File-'. $name .'-'.date('YmdHisv').'.xlsx';
  $xlsFileTemp = storage_path(config('opi.app.local_temp_folder').$fileName);

$spreadsheet = new Spreadsheet;
            $spreadsheet->setActiveSheetIndex(0);
            $sheet = $spreadsheet->getActiveSheet();
            $sheet->setTitle('Coverages');

            $sheet->getStyle("A1:K200")->applyFromArray($this->borderResults);
            $sheet->getStyle("E1:E100")->getAlignment()->setHorizontal('right');
            $sheet->getStyle("I1:I100")->getAlignment()->setHorizontal('right');

            $row = 3;
            
            // Coverages section
            $row = $this->general_information($sheet, $row, $iquote);            

            // set size cells for results
            $cellSizeArray = ['C', 'D', 'E', 'F', 'G', 'H', 'I'];
            foreach ($cellSizeArray as $key => $cell) {
                $sheet->getColumnDimension($cell)->setWidth(18);
            }

            $cellSizeArray = ['C', 'D', 'E', 'F', 'G', 'H', 'I'];
            foreach ($cellSizeArray as $key => $cell) {
                $sheet->getColumnDimension($cell)->setWidth(18);
            }

            //back to the first sheet
            $spreadsheet->setActiveSheetIndex(0);

            $writer = new Xlsx($spreadsheet);
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment; filename='.$fileName);
            header('Content-Description: '.$fileName);
            $writer->save('php://output');
 } 
catch (\Exception $e) {

   Log::error('- [Report] error in export excel '.$e->getMessage().' In Line: '.$e->getLine());
    
   return response()->json(['code' => 403, 'message' => 'Something went wrong']);
}

but in the catch it returns that error, also in the laravel log and it tells me that it is in those lines where I have the $ name, I do not know how it is generated I have tried to enter a null, an array, an object but it does not it gives that error, how could I simulate it so I can correct it or how to fix the error?

JG_GJ
  • 755
  • 1
  • 10
  • 33
  • 1
    This code is using `preg_replace` but your error is about `preg_match`. At a quick glance, I don't see anything wrong. Somewhere around your logic around `$name` you could optionally throw an exception if that variable isn't a string, with it should always be. – Chris Haas Nov 04 '20 at 15:07
  • I have uploaded the complete function and basically in that catch is where I generate the log, laravel – JG_GJ Nov 04 '20 at 16:14
  • Remove the `try/catch` and let the system crash on its own, you might get some better error messages. Also, if you can highlight the specific line in the code above somehow (like with a comment) that the error is coming from, that will help. – Chris Haas Nov 04 '20 at 17:30
  • You are passing an array to `preg_match`, but you should pass a string. The code you've uploaded is not related to the error message. – miken32 Nov 04 '20 at 18:35
  • `$name = strtolower($name);` According to the message returned by the catch it indicates that it is in this line where it fails. @miken32 How could I simulate that error? then, so I understand better how it could have been generated – JG_GJ Nov 04 '20 at 18:51
  • This is a code warning, it's not caught as an exception. try/catch has nothing to do with this. Your error message tells you the problem and what line number the problem is on. – miken32 Nov 04 '20 at 18:56
  • I do not understand well, I would only like to be able to duplicate the error, since it is a platform that is in production and I cannot understand how the error would be generated. – JG_GJ Nov 04 '20 at 20:29

0 Answers0