我在我的项目中使用Laravel-Excel package。我正在按照Laravel-Excel Documentation中提到的所有步骤进行操作,所有操作都完美无缺。
代码正常工作: -
ExamsController.php
public function uploadExamList(Request $request)
{
// Handle the import ExamList
//$examList->handleImport();
$file = \Input::file('exam_file');
\Excel::load($file, function($reader) {
$results = $reader->get();
//do what you want
});
}
此外,我已尝试按照Laravel-Excel文档中的说明实现ExcelFile injections & Import Handlers,此处代码已崩溃。
代码无效:
ExamsController.php
public function uploadExamList(ExamListImport $examList)
{
// Handle the import ExamList
$examList->handleImport();
}
ExamListImport.php
<?php
class ExamListImport extends \Maatwebsite\Excel\Files\ExcelFile {
public function getFile()
{
// Import a user provided file
$file = \Input::file('exam_file');
$fileName = $file->getClientOriginalName();
//newfilename
$newFileName = \Auth::user()->name ."_" .date("YmdHis",time()) . "_". preg_replace('/\s+/', '', $fileName) ;
//destination
$destinationPath = public_path().'/assets/files/';
//file with full storage path
$fileWithFullPath = $destinationPath . $newFileName;
$file->move($destinationPath, $newFileName);
return $fileWithFullPath;
}
}
ExamListImportHandler.php
<?php
class ExamListImportHandler implements \Maatwebsite\Excel\Files\ImportHandler {
public function handle($import)
{
\Excel::load($import, function($reader) {
$results = $reader->get();
var_dump($results);
});
return $reader;
}
}
错误: - LaravelExcelReader.php第735行中的ErrorException: realpath()期望参数1是一个有效的路径,给定对象。
请帮我解决这个问题。