0

I am trying to export more than 10k data in my excel but it show time out or sometime show not reached. What is the best process to export large amount of data in excel. In the i gave mtycontroller and export code.

controller:

function excel(){
        return (new UsersExport())->download('invoices.xlsx');
    }

in the Export folder UsersExport.php file

<?php

namespace App\Exports;

use App\Models\TEST;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\FromArray;
use Illuminate\Contracts\Queue\ShouldQueue;

class UsersExport implements  FromQuery, WithMapping, WithHeadings,ShouldQueue
{
    /**
    * @return \Illuminate\Support\Collection
    */   
    use Exportable;

    public function query()
    {
        return TEST::query()->where('status_id','!=','12');
    }
    
    public function headings(): array
    {
        return [
           'test Id', 'test Tracking Id','test Type','customer name', 'Receiver Info','Pickup','Delivery','cash','Charge','Status','cash Payment','Paid By','Request Time','Pickup Time','Delivery Time','Time Duration'
        ];
    }
    
    public function map($row): array
    {
      if($row->delivery == '0' || empty($row->delivery)){
          $delivery = (!empty($row->branch->name)?$row->branch->name:'Not Available');
      }else{
          $delivery=(!empty(\App\Models\Branch::find($row->delivery)->name)?\App\Models\Branch::find($row->delivery)->name:'Not Available');
      }  

      $picks =\App\Models\TestLog::where('courier_id',$row->id)->where('status_id','13')->first();
        return [
             $row->id,
              $row->tracking_id,
              $row->test_type->title,
              !empty($row->customer->name)?$row->customer->name:"Not Available",
              $row->receiver_name.'-- '. $row->receiver_address .'-- '.$row->receiver_phone,
              !empty($row->pickup->name)?$row->pickup->name:"Not Available",
              !empty($delivery)?$delivery:"Not Available",
             $row->cash,
             $row->pricing->price,
             $row->status->name,
             $row->cash_status,
             $row->paid_by,
            date('d-m-Y h:i A',strtotime($row->created_at)),
             !empty($picks->created_at)?date('d-m-Y h:i A',strtotime($picks->created_at)):"Not Available",
            !empty($row->delivery_date)?date('d-m-Y h:i A',strtotime($row->delivery_date)):"Not Available",
             $row->created_at->diffForHumans()
        ];
    }  
}

Code is ok. it exports data when it has less amount of data but its occur problem when it's time to export large amount of data. Please if anyone can suggest how can i solved this when it's time to export large data

1
  • 1
    It's better to export data in parts in this case Commented Sep 26, 2021 at 11:06

1 Answer 1

0

It's the user's browser declaring the timeout. If it has to wait 30 seconds or more, then it considers the site broken and shows the error screen to the user.

The answer is to prepare the export in the background, as a job. When the user requests the export - how large or small - push it off to a job. The job generates a file and saves it to the database, and sends a notification to the user that the job is complete, perhaps giving them a link to the file (which they can then download direct) or notification of where they can find the file (if past downloads are available in an admin panel, for exampl).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.