Laravel 6 pathinfo() expects parameter 1 to be string, object given

好久不见. 提交于 2021-01-29 08:44:33

问题


This is where i get the error:

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

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....

This is my controller:

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:


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

Step1: Create Import File using below command.

php artisan make:import 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
    }
}

Step3: In Controller make changes like this:

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

Reference:

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



来源:https://stackoverflow.com/questions/60612874/laravel-6-pathinfo-expects-parameter-1-to-be-string-object-given

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!