<?php
namespace App\Imports;
use App\Models\ExcelValidation;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\HasReferencesToOtherSheets;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
class Validation implements ToCollection, WithCalculatedFormulas, HasReferencesToOtherSheets
{
private $upload_id = '';
/**
* Validation constructor.
*
* @param $id
*/
public function __construct($id)
{
$this->upload_id = $id;
}
/**
* @param Collection $rows
*/
public function collection(Collection $rows)
{
ini_set("max_execution_time", "-1");
ini_set("memory_limit", "-1");
set_time_limit(0);
foreach ($rows as $row) {
if (!empty($row[6]) && !empty($row[3]) && (strtolower($row[6]) == 'accurate' || strtolower($row[6]) == 'error')) {
$obj = new ExcelValidation();
$obj->status = strtolower($row[6]);
$obj->user_id = loginId();
$obj->excel_upload_id = $this->upload_id;
$obj->day_id = (int)date('w');
$obj->week = (int)date('W');
$obj->month_id = (int)date('m');
$obj->year = (int)date('Y');
$obj->source = $row[3];
$obj->save();
}
}
}
}
This is my code. Every thing is working fine on my local machine but when try to import file on the live server it gives 503 error. Its been 3 days i am working for its solution but all in vain. I have updated the execution_time to 1600 and post_max_size to 1024mbs and everything to maximum but did'nt work. Here is the error. Service Unavailable The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later. Additionally, a 503 Service Unavailable error was encountered while trying to use an ErrorDocument to handle the request. Please help me if anyone has know the solution. Thanks
Use of WithChunkReading resole my problem. The problem is that i have excel file that have refernce with other excel files in the same file and when its calculating the formula it takes too much time of reading data. So i used "withChunkReading" in my import its works fine. Here is my code:
<?php
namespace App\Imports;
use App\Models\ExcelValidation;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\HasReferencesToOtherSheets;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
use Maatwebsite\Excel\Concerns\WithChunkReading;
class Validation implements ToCollection, WithCalculatedFormulas, HasReferencesToOtherSheets, **WithChunkReading**
{
private $upload_id = '';
/**
* Validation constructor.
*
* @param $id
*/
public function __construct($id)
{
$this->upload_id = $id;
}
public function chunkSize(): int
{
return 1000;
}
/**
* @param Collection $rows
*/
public function collection(Collection $rows)
{
$arr = [];
$count = 0;
foreach ($rows as $row) {
if (!empty($row[6]) && !empty($row[3]) && (strtolower($row[6]) == 'accurate' || strtolower($row[6]) == 'error')) {
$arr[$count]['status'] = strtolower($row[6]);
$arr[$count]['user_id'] = loginId();
$arr[$count]['excel_upload_id'] = $this->upload_id;
$arr[$count]['day_id'] = (int)date('w');
$arr[$count]['week'] = (int)date('W');
$arr[$count]['month_id'] = (int)date('m');
$arr[$count]['year'] = (int)date('Y');
$arr[$count]['source'] = $row[3];
$count++;
}
}
ExcelValidation::insert($arr);
}
}