samedi 25 septembre 2021

show timeout when try to export large data in excel using laravel

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire