maatwebsite/excel 和 \DB::commit() 的问题;
problem with maatwebsite/excel and \DB::commit();
我正在尝试将一些数据存储在数据库中,并且在我尝试下载并 excel 包含该数据的文件后立即。所以我注意到 excel 包阻止了我的提交,显然它不允许我将数据存储在数据库中。这是我的代码。
- 我正在使用 Laravel 5.5
-"maatwebsite/excel": "~2.1.0",
public function refundTicketAndGenerateExcel($transactions, $table)
{
try
{
\DB::beginTransaction();
$this->storeRefundData($transactions);
$response = $this->generateExcel($table);
\DB::commit();
return $response;
}
catch (\Exception $e)
{
\DB::rollback();
\Log::error($e);
$result['message'] = $e->getMessage();
return response()->json($result, 500);
}
}
public function generateExcel($table)
{
Excel::create('Reembolsos', function ($excel) use ($table) {
$excel->sheet('Reembolsos', function ($sheet) use ($table) {
$FontStyle = array(
'font' => array(
'name' => 'Arial',
'color' => array('rgb' => '000000'),
'size' => 11
),
);
$sheet->loadView($this->path . '.partials.excel', ['table'=>$table]);
$sheet->getStyle('A1:K1000')->applyFromArray($FontStyle);
});
})->export('xls');
}
PD:如果我只是评论 \DB::beginTransaction() 和 \DB::commit(),一切正常;另一方面,如果我只是评论 Excel::create 块,一切也都很好;这就是为什么我说 excel 包阻止了我的提交。
提前致谢。
当您调用 export() 时,它会终止您的脚本。老实说,设计相当蹩脚:
protected function _download(Array $headers = [])
{
$filename = $this->filename;
$userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
// Just for Microsoft Explore
if (preg_match('/Trident|Edge/i', $userAgent)) {
$filename = rawurlencode($filename);
}
// Set the headers
$this->_setHeaders(
$headers,
[
'Content-Type' => $this->contentType,
'Content-Disposition' => 'attachment; filename="' . $filename . '.' . $this->ext . '"',
'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', // Date in the past
'Last-Modified' => Carbon::now()->format('D, d M Y H:i:s'),
'Cache-Control' => 'cache, must-revalidate',
'Pragma' => 'public'
]
);
// Check if writer isset
if (!$this->writer)
throw new LaravelExcelException('[ERROR] No writer was set.');
// Download
$this->writer->save('php://output');
// End the script to prevent corrupted xlsx files
exit;
}
通过返回 Excel::create()
returns 的编写器对象并删除 ->export()
部分来解决此问题。
$writer = Excel::create(blah blah blah); // no->export() now!
return $writer;
然后在提交后进行导出。
$writer = $this->generateExcel($table);
\DB::commit();
return $writer->export();
我正在尝试将一些数据存储在数据库中,并且在我尝试下载并 excel 包含该数据的文件后立即。所以我注意到 excel 包阻止了我的提交,显然它不允许我将数据存储在数据库中。这是我的代码。 - 我正在使用 Laravel 5.5 -"maatwebsite/excel": "~2.1.0",
public function refundTicketAndGenerateExcel($transactions, $table)
{
try
{
\DB::beginTransaction();
$this->storeRefundData($transactions);
$response = $this->generateExcel($table);
\DB::commit();
return $response;
}
catch (\Exception $e)
{
\DB::rollback();
\Log::error($e);
$result['message'] = $e->getMessage();
return response()->json($result, 500);
}
}
public function generateExcel($table)
{
Excel::create('Reembolsos', function ($excel) use ($table) {
$excel->sheet('Reembolsos', function ($sheet) use ($table) {
$FontStyle = array(
'font' => array(
'name' => 'Arial',
'color' => array('rgb' => '000000'),
'size' => 11
),
);
$sheet->loadView($this->path . '.partials.excel', ['table'=>$table]);
$sheet->getStyle('A1:K1000')->applyFromArray($FontStyle);
});
})->export('xls');
}
PD:如果我只是评论 \DB::beginTransaction() 和 \DB::commit(),一切正常;另一方面,如果我只是评论 Excel::create 块,一切也都很好;这就是为什么我说 excel 包阻止了我的提交。
提前致谢。
当您调用 export() 时,它会终止您的脚本。老实说,设计相当蹩脚:
protected function _download(Array $headers = [])
{
$filename = $this->filename;
$userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
// Just for Microsoft Explore
if (preg_match('/Trident|Edge/i', $userAgent)) {
$filename = rawurlencode($filename);
}
// Set the headers
$this->_setHeaders(
$headers,
[
'Content-Type' => $this->contentType,
'Content-Disposition' => 'attachment; filename="' . $filename . '.' . $this->ext . '"',
'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', // Date in the past
'Last-Modified' => Carbon::now()->format('D, d M Y H:i:s'),
'Cache-Control' => 'cache, must-revalidate',
'Pragma' => 'public'
]
);
// Check if writer isset
if (!$this->writer)
throw new LaravelExcelException('[ERROR] No writer was set.');
// Download
$this->writer->save('php://output');
// End the script to prevent corrupted xlsx files
exit;
}
通过返回 Excel::create()
returns 的编写器对象并删除 ->export()
部分来解决此问题。
$writer = Excel::create(blah blah blah); // no->export() now!
return $writer;
然后在提交后进行导出。
$writer = $this->generateExcel($table);
\DB::commit();
return $writer->export();