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

Posted

技术标签:

【中文标题】Laravel 6 pathinfo() 期望参数 1 是字符串,给定对象【英文标题】:Laravel 6 pathinfo() expects parameter 1 to be string, object given 【发布时间】:2020-03-10 07:06:56 【问题描述】:

这是我得到错误的地方:

$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');


    
     

```


【问题讨论】:

【参考方案1】:

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

参考:

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

【讨论】:

我试过了,但它导致我出现另一个错误 Symfony\Component\Debug\Exception\FatalThrowableError Too little arguments to function Maatwebsite\Excel\Excel::import(), 1 pass in C:\xampp \htdocs\BarangayInformationDesk\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php 在第 261 行,预计至少 2 个 谢谢,您提供的代码有效,导入问题已解决。仍然有一些问题,但我找不到解决这些问题的方法。谢谢

以上是关于Laravel 6 pathinfo() 期望参数 1 是字符串,给定对象的主要内容,如果未能解决你的问题,请参考以下文章

如何在laravel中获取数组元素的文件扩展名

htmlspecialchars() 期望参数 1 是字符串,给定对象 - laravel

Laravel 5:in_array() 期望参数 2 是数组,给定对象

Laravel - htmlspecialchars() 期望参数 1 是字符串,给定对象

Laravel - strtolower() 期望参数 1 是字符串,给定对象

strpos() 期望参数 1 是字符串,对象给定 laravel 5.5