我正在尝试从excel导入100k数据到数据库,并且遇到超时问题,因此我使用了块方法,但是它说“块方法不存在”。 我该如何解决这个问题?
Excel::filter('chunk')->load($request->file('excelFile')->chunk(900,
function ($reader) use ($electionId, &$voterCount, &$lastVoter,&$rowError)
{
foreach ($reader->toArray()[0] as $row) {
$voter = new Voter();
$voter->electionId = $electionId;
$voter->voterNumber = $row['voter_id'];
$voter->name = $row['name'];
$voter->password = $row['password'] ===
null || $row['password'] == '' ? rand(1,100000) : $row['password'];
$voter->emailAddress = $row['email'];
$voter->mobileNumber = $row['mobile_number'];
}
}));
答案 0 :(得分:0)
我已上传经销商详细信息。试试这个,这对我来说是有效的:
public function addDealerByAdmin(Request $request_body)
{
if ($request_body->hasFile('file')) {
//if ($_FILES['file']['type'] == "text/csv" || $csv_file_type = array('text/csv')) {
$file_array = Input::file('file');
$file_name = $file_array->getClientOriginalName();
//Log::info('file_type : ', ['file_type' => $_FILES['file']['tmp_name'], 'file_ext' => File::extension($file_name)]);
if (File::extension($file_name) == 'csv') {
// Good to go
$csv_file_type = array('text/csv');
$csvMimes = array('application/vnd.ms-excel');
$filename = $_FILES["file"]["tmp_name"];
$file_type = $_FILES['file']['type'];
$csvFile = fopen($filename, 'r');
//skip first line
fgetcsv($csvFile);
DB::beginTransaction();
$i = 2;
//parse data from csv file line by line
DB::delete('DELETE FROM dealer_master');
while (($line = fgetcsv($csvFile)) !== FALSE) {
$company_name = isset($line[0]) ? $line[0] : '';
$address_line_1 = isset($line[1]) ? $line[1] : '';
$city = isset($line[2]) ? $line[2] : '';
$state_or_province = isset($line[3]) ? $line[3] : '';
$zip_or_postal_code = isset($line[4]) ? $line[4] : '';
$phone = isset($line[5]) ? $line[5] : '';
$client_id = isset($line[6]) ? $line[6] : '';
$sales_representative = isset($line[7]) ? $line[7] : '';
$dealer_OEM_franchise = isset($line[8]) ? $line[8] : '';
$validation_data = array(
'company_name' => $company_name,
'address_line_1' => $address_line_1,
'city' => $city,
'state_or_province' => $state_or_province,
'zip_or_postal_code' => $zip_or_postal_code,
);
//Log::info('validation', ['validation' => $validation_data]);
// (new VerificationController())->required_validation($validation_data, $i);
$count = 0;
foreach ($validation_data as $key => $value) {
if (empty($value)) {
//return Response::json(array('code' => '201', 'message' => 'Please enter ' . $key . ' at line ' . $i . '.', 'cause' => '', 'response' => json_decode("{}")));
$count = $count + 1;
}
}
if ($count == 0) {
$data = array(
'company_name' => $company_name,
'address_line_1' => $address_line_1,
'city' => $city,
'state_or_province' => $state_or_province,
'zip_or_postal_code' => $zip_or_postal_code,
'phone' => $phone,
'client_id' => $client_id,
'sales_representative' => $sales_representative,
'dealer_OEM_franchise' => $dealer_OEM_franchise,
'is_active' => 1,
'create_time' => $create_time
);
DB::beginTransaction();
$dealer_id = DB::table('dealer_master')->insertGetId($data);
DB::commit();
}
$i++;
}
//$i++;
DB::commit();
//close opened csv file
fclose($csvFile);
$response = Response::json(array('code' => '200', 'message' => 'Dealer added successfully.', 'cause' => '', 'response' => json_decode("{}")));
} else {
//Log::info('file_type', ['file_type' => $_FILES['file']['type']]);
return $response = Response::json(array('code' => '201', 'message' => 'You can upload only CSV file.', 'cause' => '', 'response' => json_decode("{}")));
}
} else {
return $response = Response::json(array('code' => '201', 'message' => 'Required field file is missing or empty.', 'cause' => '', 'response' => json_decode("{}")));
}
}