Laravel 6 pathinfo()期望参数1为字符串,给定对象



这就是我得到错误的地方:

$data = Excel::import($path, function($reader) {})->get();

我把load()改成了import()。我想在Laravel 6中运行这段代码,但MaatWebsiteExcel的第3版不支持load()。我一直在寻找解决方案,但找不到。。。。

这是我的控制器:

namespace AppHttpControllers;
use AppContact;
use AppCsvData;
use AppHttpRequestsCsvImportRequest;
use IlluminateHttpRequest;
use MaatwebsiteExcelFacadesExcel;
use Session;
use DB;
class ImportController extends Controller
{
public function getImport()
{
return view('import');
}
function parseImport(CsvImportRequest $request)
{
$path = $request->file('csv_file')->getRealPath();
if ($request->has('header')) {
$data = Excel::import($path, function($reader) {})->get();
} else {
$data = array_map('str_getcsv', file($path));
}
if (count($data) > 0) {
if ($request->has('header')) {
$csv_header_fields = [];
foreach ($data[0] as $key => $value) {
$csv_header_fields[] = $key;
}
}
$csv_data = array_slice($data, 0, 2);
$credentials = $request->file('csv_file')->getClientOriginalName();
$filename = CsvData::all('csv_filename');

if(CsvData::where('csv_filename', '=' ,$credentials)->exists()){
return redirect()->back()->with('alert', 'This specific file has already been imported!');
}
else{
$csv_data_file = CsvData::create([
'csv_filename' => $request->file('csv_file')->getClientOriginalName(),
'csv_header' => $request->has('header'),
'csv_data' => json_encode($data)
]);
}
}
else {
return redirect()->back();
}
return view('import_fields', compact( 'csv_header_fields', 'csv_data', 'csv_data_file'));
}
public function processImport(Request $request)
{
$data = CsvData::find($request->csv_data_file_id);
$csv_data = json_decode($data->csv_data, true);
if(CsvData::where('csv_data', '=' ,$csv_data)->exists()){
return redirect()->back()->with('alert', 'This file has already been imported!');
}
else{
foreach ($csv_data as $row) {
$contact = new Contact();
foreach (config('app.db_fields') as $index => $field) {
if ($data->csv_header) {
$contact->$field = $row[$request->fields[$field]];
} else {
$contact->$field = $row[$request->fields[$index]];
}
}
foreach($contact as $contacts){
$contact->posted_by             = $contacts->posted_by;
$contact->employer              = $contacts->employer;
$contact->address               = $contacts->address;
$contact->barangay              = $contacts->barangay;
$contact->citymunicipality      = $contacts->citymunicipality;
$contact->province              = $contacts->province;
$contact->region                = $contacts->region;
$contact->position              = $contacts->position;
$contact->job_description       = $contacts->job_description;
$contact->salary                = $contacts->salary;
$contact->count                 = $contacts->count;
$contact->work_location         = $contacts->work_location;
$contact->nature_of_work        = $contacts->nature_of_work;
$contact->min_work_exp_mos      = $contacts->min_work_exp_mos;
$contact->min_educ_level        = $contacts->min_educ_level;
$contact->coursemajor           = $contacts->coursemajor;
$contact->min_age               = $contacts->min_age;
$contact->max_age               = $contacts->max_age;
$contact->min_height            = $contacts->min_height;
$contact->sex                   = $contacts->sex;
$contact->civil_status          = $contacts->civil_status;
$contact->other_qualifications  = $contacts->other_qualifications;
$contact->remarks               = $contacts->remarks;
$contact->accept_disability     = $contacts->accept_disability;
$contact->date_posted           = $contacts->date_posted['date'];
$contact->valid_until           = $contacts->valid_until['date'];
$contact->date_created          = $contacts->date_created['date'];
$contact->last_modified_by      = $contacts->last_modified_by['date'];
$contact->date_last_modified    = $contacts->date_last_modified['date'];
}
$contact->save();
return view('import_success');

}
}
}
}```

import()方法的第一个参数不再是Laravel 3.1中文件的路径,而是必须创建的Import文件的类名。您需要按照以下步骤使用import方法

步骤1:使用以下命令创建导入文件。

php artisan make:import CsvImport

步骤2:内部CsvImport进行如下更改:

namespace AppImports;
use IlluminateSupportCollection;
use MaatwebsiteExcelConcernsToCollection;
class CsvImport implements ToCollection
{
public function collection(Collection $rows)
{
return $rows; //add this line
}
}

步骤3:在控制器中进行如下更改:

$path = $request->file('csv_file')->getRealPath();
$rows = Excel::import(new CsvImport, $path);

参考:

https://docs.laravel-excel.com/3.1/imports/basics.html

我只需要特定索引后的那些列,我认为这实际上与本页顶部问题中描述的用例相同。我提出以下解决方案。

$fileArr = (new BulkSampleImport)->toArray($request->file);

相关内容

  • 没有找到相关文章

最新更新