我想将我的excel(XLSX)文件导入我的数据库。在我的文件中有3张纸,每张纸有42000到55000行和11列。
我正在使用Maatwebsite / Laravel-Excel
什么时候上传我的文件时得到错误
Serialization of closure failed: Serialization of 'Closure' is not allowed
我的代码是
Excel::filter('chunk')->load($file)->chunk(250, function($reader) use ($request)
{
$filePath = $request->file_name;
$getFileInfo = session()->get('fileinfo');
$insertFileInfo = FileInfo::firstOrCreate($getFileInfo);
$getCaseID = $insertFileInfo->case_id;
//dd($insertFileInfo);
$datetime = $request->datetime;
$aparty = $request->aparty;
$bparty = $request->bparty;
$call_duration = $request->call_duration;
$usage_type = $request->usage_type;
$lac = $request->lac;
$cell = $request->cell;
$imei = $request->imei;
$imsi = $request->imsi;
$address = $request->address;
$network_type = $request->network_type;
foreach ($reader->toArray() as $row) {
foreach ($row as $item) {
if (!empty($item[$datetime]) && ($item[$datetime] != $datetime)) {
$data = [
'case_id' => $getCaseID,
'datetime' => $datetime == "NO Data Field" ? '' : Carbon::parse(self::clean($item[$datetime]))->toDateTimeString(),
'aparty' => $aparty == "NO Data Field" ? '' : self::clean($item[$aparty]),
'bparty' => $bparty == "NO Data Field" ? '' : self::clean($item[$bparty]),
'call_duration' => $call_duration == "NO Data Field" ? '0' : str_replace('second(s)', '', $item[$call_duration]) == '' ? '0' : str_replace('second(s)', '', $item[$call_duration]),
'usage_type' => $usage_type == "NO Data Field" ? 'Undefined' : str_replace(' ', '_', $item[$usage_type]),
'lac' => $lac == "NO Data Field" ? self::convertCellToLac($item[$cell]) : $item[$lac],
'cell' => $cell == "NO Data Field" ? self::convertCellFormat($item[$cell]) : self::convertCellFormat($item[$cell]),
'imei' => $imei == "NO Data Field" ? 'Undefined' : $item[$imei] == '' ? 'Undefined' : $item[$imei],
'imsi' => $imsi == "NO Data Field" ? 'Undefined' : $item[$imsi] == '' ? 'Undefined' : $item[$imsi],
'address' => $address == "NO Data Field" ? 'Undefined' : $item[$address] == '' ? 'Undefined' : $item[$address],
'network_type' => empty($item[$network_type]) ? 'Undefined' : $network_type == "NO Data Field" ? 'Undefined' : $item[$network_type]
];
try {
CallCdr::firstOrCreate($data);
} catch (\Exception $e) {
}
}
}
}
});
return redirect()->route('home');
我没有使用use ($request)
然后这个错误消失但我需要所有请求参数。
也不是重定向。
答案 0 :(得分:0)
这是预期的行为。您无法序列化请求。
要解决此问题,您只需将请求中的相关详细信息分配给单独的变量,然后将其传递到chunk()
。
$inputs = $request->all(); // returns an array
Excel::filter('chunk')->load($file)->chunk(250, function($reader) use ($inputs) {
....
}