我在项目中使用Laravel Excel包。我正在遵循Laravel Excel文档中提到的所有步骤,所有的事情都很完美。
代码正在工作:-
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
});
}
此外,我还尝试实现ExcelFile注入&LaravelExcel文档中提到的导入处理程序,这里的代码崩溃了。
代码不工作:-
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行出现错误异常:realpath()要求参数1是一个有效的路径,给定对象。
请在这个问题上帮助我。
我认为这里的"$$reader"应该是"$reader":
ExamListImportHandler.php
类ExamListImportHandler实现''Maatewebsite''Excel''Files''ImportHandler{
public function handle($import)
{
'Excel::load($import, function($reader) {
$results = $$reader->get();
var_dump($results);
});
return $reader;
}
}