Return 来自 Laravel Excel 中的变量导入
Return variable from within Laravel Excel Import
下面的代码,基本示例,但我想 return 错误(请参阅导入后注释掉的 if 语句)。我如何将 'import' class 中的变量传递回控制器?
控制器:
public function resultsImport(Request $request)
{
//validate the xls file
$this->validate($request, array('student_results_import_file' => 'required'));
if($request->hasFile('student_results_import_file')){
$extension = File::extension($request->student_results_import_file->getClientOriginalName());
if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {
Excel::import(new StudentResultsImport, $request->file('student_results_import_file'));
// if $ignore_count>0 --> do stuff
return redirect('/baadmin/students')->with('flash_message', ['success','Results Imported Successfully','Student file "'. $request->student_results_import_file->getClientOriginalName() .'" imported successfully!']);
} else {
return redirect('/baadmin/students')->with('flash_message', ['error','Results Import Failed','Student file "'. $request->student_results_import_file->getClientOriginalName() .'" import failed. File is a '. $extension .' file. Please upload a valid xls/csv file.']);
}
}
}
导入
public function collection(Collection $rows)
{
set_time_limit(300);
$ignore_count = 0;
foreach ($rows as $row) {
if ($row[8] != '') {
//import
}
else {
$ignore_count++;
//$ignore_count is just a simple example, ultimately would probably have an array of the various errors detected on the various rows
}
}
}
设法在文档中找到合适的答案,参考'Getters':
https://docs.laravel-excel.com/3.1/architecture/objects.html#getters
例如:
namespace App\Imports;
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
class UsersImport implements ToModel
{
private $rows = 0;
public function model(array $row)
{
++$this->rows;
return new User([
'name' => $row[0],
]);
}
public function getRowCount(): int
{
return $this->rows;
}
}
导入完成后,我们可以用getter请求状态。
$import = new UsersImport;
Excel::import($import, 'users.xlsx');
dd('Row count: ' . $import->getRowCount());
您可以使用下面的代码。
namespace App\Imports;
使用App\User;
使用 Maatwebsite\Excel\Concerns\ToModel;
class UsersImport 实现 ToModel
{
私人 $rows = 0;
public function model(array $row)
{
++$this->rows;
return new User([
'name' => $row[0],
]);
}
public function getRowCount(): int
{
return $this->rows;
}
}
$import = new UsersImport;
Excel::import($import, 'users.xlsx');
dd('Row count: ' . $import->getRowCount());
更多信息:https://slaford.com/excel/return-variable-from-within-laravel-excel-import/
下面的代码,基本示例,但我想 return 错误(请参阅导入后注释掉的 if 语句)。我如何将 'import' class 中的变量传递回控制器?
控制器:
public function resultsImport(Request $request)
{
//validate the xls file
$this->validate($request, array('student_results_import_file' => 'required'));
if($request->hasFile('student_results_import_file')){
$extension = File::extension($request->student_results_import_file->getClientOriginalName());
if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {
Excel::import(new StudentResultsImport, $request->file('student_results_import_file'));
// if $ignore_count>0 --> do stuff
return redirect('/baadmin/students')->with('flash_message', ['success','Results Imported Successfully','Student file "'. $request->student_results_import_file->getClientOriginalName() .'" imported successfully!']);
} else {
return redirect('/baadmin/students')->with('flash_message', ['error','Results Import Failed','Student file "'. $request->student_results_import_file->getClientOriginalName() .'" import failed. File is a '. $extension .' file. Please upload a valid xls/csv file.']);
}
}
}
导入
public function collection(Collection $rows)
{
set_time_limit(300);
$ignore_count = 0;
foreach ($rows as $row) {
if ($row[8] != '') {
//import
}
else {
$ignore_count++;
//$ignore_count is just a simple example, ultimately would probably have an array of the various errors detected on the various rows
}
}
}
设法在文档中找到合适的答案,参考'Getters': https://docs.laravel-excel.com/3.1/architecture/objects.html#getters
例如:
namespace App\Imports;
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
class UsersImport implements ToModel
{
private $rows = 0;
public function model(array $row)
{
++$this->rows;
return new User([
'name' => $row[0],
]);
}
public function getRowCount(): int
{
return $this->rows;
}
}
导入完成后,我们可以用getter请求状态。
$import = new UsersImport;
Excel::import($import, 'users.xlsx');
dd('Row count: ' . $import->getRowCount());
您可以使用下面的代码。
namespace App\Imports;
使用App\User; 使用 Maatwebsite\Excel\Concerns\ToModel;
class UsersImport 实现 ToModel { 私人 $rows = 0;
public function model(array $row)
{
++$this->rows;
return new User([
'name' => $row[0],
]);
}
public function getRowCount(): int
{
return $this->rows;
}
}
$import = new UsersImport;
Excel::import($import, 'users.xlsx');
dd('Row count: ' . $import->getRowCount());
更多信息:https://slaford.com/excel/return-variable-from-within-laravel-excel-import/