Laravel 6 pathinfo() 期望参数 1 为字符串,给定的对象
Laravel 6 pathinfo() expects parameter 1 to be string, object given
这是我收到错误的地方:
$data = Excel::import($path, function($reader) {})->get();
我将 load()
更改为 import()
。我想运行这段代码在Laravel 6,但是MaatWebsiteExcel 3版本不支持load()
。
我一直在寻找解决方案,但我找不到任何....
这是我的控制器:
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');
}
}
}
}```
import()
方法的第一个参数不再是Laravel 3.1
中的文件路径,而是Import
文件的class名称创造。
您需要按照以下步骤使用 import
方法
第 1 步: 使用以下命令创建导入文件。
php artisan make:import CsvImport
第 2 步: 在 CsvImport 中进行如下更改:
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 中进行如下更改:
$path = $request->file('csv_file')->getRealPath();
$rows = Excel::import(new CsvImport, $path);
参考:
这是我收到错误的地方:
$data = Excel::import($path, function($reader) {})->get();
我将 load()
更改为 import()
。我想运行这段代码在Laravel 6,但是MaatWebsiteExcel 3版本不支持load()
。
我一直在寻找解决方案,但我找不到任何....
这是我的控制器:
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');
}
}
}
}```
import()
方法的第一个参数不再是Laravel 3.1
中的文件路径,而是Import
文件的class名称创造。
您需要按照以下步骤使用 import
方法
第 1 步: 使用以下命令创建导入文件。
php artisan make:import CsvImport
第 2 步: 在 CsvImport 中进行如下更改:
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 中进行如下更改:
$path = $request->file('csv_file')->getRealPath();
$rows = Excel::import(new CsvImport, $path);
参考: