0

I am getting error when i am Importing data from Excel Sheet, I am doing it by Controller. and I am getting this error Call to undefined method Maatwebsite\Excel\Excel::load()

Please let me know if it can be possible by Model,....

Here are my Model Lead.php

<?php
  namespace App;
  use Illuminate\Database\Eloquent\Model;
  class Lead extends Model
 {
 protected $primaryKey = 'lId';
}

Here are my Controller LeadController.php

public function import(Request $request){
$this->validate($request, [
    'select_file' => 'required|mimes:xls, xlsx'
]);
$path=$request->file('select_file')->getRealPath();
$data=Excel::load($path)->get();
if($data->count() > 0)
{
    foreach ($data->toArray() as $key => $value) 
    {
        foreach($valuse as $row)
        {
            $insert_data[]=array(
                'Name'=>$row['name'],
                'Project'=>$row['project'],
                'Follow up Date'=>$row['followup'],
                'Lead Source'=>$row['lead_source'],
                'Email Id'=>$row['email'],
                'Message'=>$row['message'],
                'Phone Number'=>$row['number'],
                'Status'=>$row['status'],
            );
        }
    }
    if(!empty($insert_data))
    {
        DB::table('leads')->insert($insert_data);
    } 
}
return back()->with('success', 'Excel Data Imported successfully.');
}

And here are my View code...

<form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
                            @csrf
                            <input type="file" name="select_file" class="form-control">
                            <br>
                            <input type="submit" name="upload" class="btn btn-primary" value="Upload">
</form>

Here are my Web.php

Route::post('lead/import', 'LeadController@import')->name('import');
saini tor
  • 223
  • 6
  • 21

2 Answers2

0

As per Laravel Excel 3 documentation changes

Excel::load() is removed and replaced by Excel::import($yourImport)

So resolve this error Either use version 2.x as below

composer require "maatwebsite/excel:~2.1.0"

OR

Use import() method of version 3

You need to follow below steps to use import method of version3

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);
Sehdev
  • 5,486
  • 3
  • 11
  • 34
  • I am getting this error after using `import()`...`Too few arguments to function Maatwebsite\Excel\Excel::import(), 1 passed in C:\xampp\htdocs\group3\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 261 and at least 2 expected` – saini tor Mar 25 '20 at 11:44
  • @sainitor You can follow this tutorial to make it work https://stackoverflow.com/questions/60612874/laravel-6-pathinfo-expects-parameter-1-to-be-string-object-given/60613451#60613451 – Sehdev Mar 25 '20 at 13:01
0

I done the import function before and meet this error also. You can either use version 2.x which mentioned by Sehdev :

  1. delete the config/excel.php
  2. in your composer.json, change "maatwebsite/excel": "^3.1" to "maatwebsite/excel": "^2.1"
  3. run composer update

Or if you insist to use it in version 3.1 like me, this is how I solve it.
Use :

$data = Excel::toArray(new ContactImport,$request->file('select_file'))[0];

Instead of :

$data=Excel::load($path)->get();

ContactImport is the import class I created via PHP artisan make: import ContactImport --model=User by following the 5 minutes quick start in Laravel Excel documentation.

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

I am a Laravel newbie. Hope my suggestion can solve your problem.

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Darren
  • 23
  • 2
  • 5