0

I am using PHPOffice for exporting excel in laravel it work fine on localhost but it gives me internal server error on live server error?

public function export(Request $request){
   $branchId =  $this->decryption($request->branch_id);
   $data = Reservation::select("id", "name", "contact","start_time","end_time","target_date",'weekdays')->with('pages',function($query){
                $query->select('pages.id','name');
            })->when($request->has('start_date'),function($query) use ($request){
                $query->where('target_date','>=',$request->start_date);
            })->when($request->has('end_date'),function($q) use ($request){
                $q->where('target_date','<=',$request->end_date);
            })
            ->where('branch_id',$branchId)->get();
        $data_array [] = array("name","contact","target_date","start_time","end_time","weekdays","page");
        foreach($data as $data_item)
        {
            $data_array[] = array(
                'name'      =>$data_item->name,
                'contact'   => $data_item->contact,
                'target_date' => $data_item->target_date,
                'start_time' => $data_item->start_time,
                'end_time' => $data_item->end_time,
                'weekdays' => implode($data_item->weekdays),
                'page' =>$data_item['pages'][0]['name']
            );
        }
         $this->ExportExcel($data_array);
    }

This funciton is used to export data which we have got from export function

public function ExportExcel($customer_data){

        ini_set('max_execution_time', 0);
        ini_set('memory_limit', '4000M');
        try {
            $spreadSheet = new Spreadsheet();
            $spreadSheet->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);
            $spreadSheet->getActiveSheet()->fromArray($customer_data, NULL, 'A1');
            $Excel_writer = new Xls($spreadSheet);
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="Customer_ExportedData.xls"');
            header('Cache-Control: max-age=0');
            ob_end_clean();
            $Excel_writer->save('php://output');
            exit();
        } catch (Exception $e) {
            return $e;
        }
    }`

This is what i am getting error at the end in respose

status code :502
{
  "message": "Internal server error"
}
  • 2
    `ini_set('memory_limit', '4000M');` this is a very bad practice and can be easily exploited by the end user. So, be careful with your implementations. Just a few bot calls on the download endpoint might take your full server memory if it actually takes memory up to GB. It is better to perform this on a scheduler or background queues. – Anuj Shrestha Feb 15 '23 at 11:14
  • 1
    Also add what error you are getting on the server, check your larvael log file – Anuj Shrestha Feb 15 '23 at 11:15
  • @AnujShrestha thanks giving me advice. I have added response in the end kindly check and see, how can you help me to figure this out! I have checked logs also, i couldn't find anything which is relevant to this. – Ahmad Bajwa Feb 15 '23 at 11:23
  • Not the response. The error will be logged at storage/logs/laravel.log (if not used daily) – Anuj Shrestha Feb 15 '23 at 11:37
  • @AnujShrestha Bro, I have checked in error log present in storage folder which you have mentioned but don't have anything like that. Application is running on aws and i have also checked over there. I dont have anything why this is happening? – Ahmad Bajwa Feb 16 '23 at 05:16
  • My bad I didn't see your try catch above. You are catching the error so it won't be logged. Add an `info('Export Error', [$e]);` line in your catch block before `return`. Then the error will be logged in your laravel log. – Anuj Shrestha Feb 16 '23 at 05:20
  • @AnujShrestha i have tried which you have mentioned above comment ```info('Export Error', [$e]);``` but didn't work on live site. no logs are printing in the file – Ahmad Bajwa Feb 16 '23 at 06:20

0 Answers0