Bachar ELkarni
Bachar ELkarni

Reputation: 73

Laravel's maatwebsite/excel import problem

first of all thank you for giving the time to read this, hope you are doing fine. I have a problem with a project I am making using laravel. One of the functionalities of this project is that it can send an email to different mail @ that it has to get from an excel file. so naturally I went with the Maatwebsite package being one of the most popular choices out there. So the problem is that my imports file and also my controller can't recognize the package when I mention it in the use section. I get the following errors:

In the import file: 'userImport.php':

In the Controller:

I'm sorry if my explanation is a bit lacking, cuz this whole framework thing is new to me but I guess you will get it in the code below.

UsersImport.php

<?php

namespace App\Imports;

use App\Models\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class UsersImport implements ToModel, WithHeadingRow 
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new User([
            "first_name" => $row[0],
            "last_name" => $row[1],
            "email" => $row[2],
        ]);
    }
}

The controller file:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Imports\UsersImport;
use Maatwebsite\Excel\Excel;

class invitationController extends Controller
{

    public function import(Request $request)
    {
        Excel::import(new UsersImport, $request->file);
       
    }
}

Upvotes: 0

Views: 5438

Answers (1)

Bachar ELkarni
Bachar ELkarni

Reputation: 73

Solved it by removing the package using the following command: composer remove maatwebsite/excel Then re-install it using: composer require psr/simple-cache:^1.0 maatwebsite/excel --ignore-platform-reqs instead of: composer install maatwebsite/excel

Upvotes: 3

Related Questions