Laravel 6 pathinfo()期望参数1是字符串,给定对象 [英] Laravel 6 pathinfo() expects parameter 1 to be string, object given
问题描述
这是我得到错误的地方:
This is where i get the error:
$ data = Excel :: import($ path,function($ reader){})-> get();
我将 load()
更改为 import()
.我想在Laravel 6中运行此代码,但是MaatWebsiteExcel的版本3不支持 load()
.我一直在寻找解决方案,但找不到任何解决方案....
I changed the load()
to import()
. I want to run this code in Laravel 6, but version 3 of MaatWebsiteExcel does not support load()
.
I've been searching for solutions, yet i cant find any....
这是我的控制者:
namespace App\Http\Controllers;
use App\Contact;
use App\CsvData;
use App\Http\Requests\CsvImportRequest;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
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');
}
}
}
}```
推荐答案
在 Laravel 3.1
中, import()
方法的第一个参数不再是文件的路径.>,但必须创建 Import
文件的类名.您需要按照以下步骤使用 import
方法
The first parameter of the import()
method is not the path to the file anymore in Laravel 3.1
, but the class name of the Import
file you have to create.
You need to follow below steps to use import
method
第1步:使用以下命令创建导入文件.
Step1: Create Import File using below command.
php artisan make:import CsvImport
第二步:在CsvImport内部进行如下更改:
Step2: Inside CsvImport make changes like this:
namespace App\Imports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class CsvImport implements ToCollection
{
public function collection(Collection $rows)
{
return $rows; //add this line
}
}
第3步:在Controller中进行如下更改:
Step3: In Controller make changes like this:
$path = $request->file('csv_file')->getRealPath();
$rows = Excel::import(new CsvImport, $path);
参考:
https://docs.laravel-excel.com/3.1/imports/basics.html
这篇关于Laravel 6 pathinfo()期望参数1是字符串,给定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!