Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

Call to undefined method Maatwebsite\Excel\Excel::load() laravel uploading with excel data into table

guys working with online exam project. i need to add students data bulk.

so i planned to using maatwebsite/excel

i saw composer show in my gitbash it showed like below.

maatwebsite/excel                     3.1.19    Supercharged Excel exports...

but in my composer.json

  "require": {
        "php": "^7.2",
        "barryvdh/laravel-dompdf": "^0.8.6",
        "fideloper/proxy": "^4.0",
        "filp/whoops": "^2.7",
        "intervention/image": "^2.5",
        "laravel/framework": "^6.2",
        "laravel/tinker": "^2.0",
        "realrashid/sweet-alert": "^3.1",
        "spatie/laravel-permission": "^3.11",
        "uxweb/sweet-alert": "^2.0",
        "yajra/laravel-datatables-buttons": "^4.0",
        "yajra/laravel-datatables-editor": "^1.19",
        "yajra/laravel-datatables-fractal": "^1.5",
        "yajra/laravel-datatables-html": "^4.22",
        "yajra/laravel-datatables-oracle": "^9.8"

    },

My first confusion is whether that package is there are not in my project?

next i tried to upload excel data this is my coding

  $this->validate($request, [
            'student_excel_file'  => 'required|mimes:xls,xlsx'
           ]);       

           $path = $request->file('student_excel_file')->getRealPath();       

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

           dump( $data);

this shows error

Call to undefined method Maatwebsite\Excel\Excel::load()

when i google most of them said in maatwebsite version 3 and above load is depreciated. so most of them said to switch back to version 2. iam unable to do.

i ran composer remove maatwesbite/excel. but i cant see it in composer.json. but still the package exist. what should i do??

let me say my process. i need to get student_name, student_username, student_password from form and it will be stored in StudentRegistration table and then it is stored in user table with userprofile as polymorph table

this is my coding

$data = $request->all();
        foreach ($data['student_name'] as $i => $name) {
            $addstudent = new StudentRegistration([
                'login_user_id' => auth()->id(),
                'school_id' => $request->school_id,
                'class_id' => $request->class_id,
                'section_id' => $request->section_id,
                'stud_name' => $name,               
                'stud_status' => "active",
            ]);
            $addstudent->save();

            $addstudent->user()->create(
                ['name' => $name,
                'username' =>  $data['username'][$i],
                'user_type' => "student",
                'password' =>  $data['password'][$i],
                'status' => "active"
            ]);
        }

      alert()->success('Student Added Successfully');


Now i need to do the same process from excel sheet.. how can i do that

i referred this link for uploading using excel sheet: https://www.webslesson.info/2019/02/import-excel-file-in-laravel.html

kindly some one suggest please..

0 likes
5 replies
AbdulBazith's avatar

@deepu07 thank you for your response .

this is my controller

 $data=$request->all();        

           $filenaeeee = $request->file('student_excel_file');

           Excel::import(new BukStudentsImport($data),  $filenaeeee);

i tired like this. this is my BukStudentsImport


<?php

namespace App\Imports;

use App\StudentRegistration;
use App\User;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class BukStudentsImport implements ToCollection
{

    // public function  __construct($data)
    // {
    //     // dd($cartage);
    //     $this->data = $data;
    // }

    /**
     * @param Collection $collection
     */
    public function collection(Collection $collection)
    {

        foreach ($collection as $row) {

         $addstudent = new StudentRegistration([
                'login_user_id' => auth()->id(),
                'school_id' => $this->data['school_id'],
                'class_id' => $this->data['class_id'],
                'section_id' => $this->data['section_id'],
                'stud_name' => $row[0],
                'stud_email' => "-",
                'stud_phno' => 123,
                'stud_type' => "-",
                'stud_father' => "-",
                'stud_mother' => "-",
                'stud_id' => $row[1],
                'stud_status' => "active",
            ]);

             $addstudent->save();

            dd("STOP inserted");

            $addstudent->user()->create(
                ['name' => $row[0],
                    'username' => $row[1],
                    'user_type' => "student",
                    'password' => $row[2],
                    'status' => "active",
                ]);

        
        }
    }
}


But its not working. i can see the stop message id dd(). but the data is not inserted. why????

MarianoMoreyra's avatar

@abdulbazith if you remove the dd() sentence it doesn't work either?

That library uses database transactions, so I guess that dd() it will make the transaction to rollback.

AbdulBazith's avatar

@deepu07 @marianomoreyra thank you guys thank you so much

the issue is i have empty sheets in my excel thats why it not worked.

what i did is

foreach ($collection as $row) {
            if ($row->filter()->isNotEmpty()) {
                $addstudent = new StudentRegistration([
                'login_user_id' => auth()->id(),
                'school_id' => $this->data['school_id'],
                'class_id' =>$this->data['class_id'],
                'section_id' => $this->data['section_id'],
                'stud_name' => $row[0],
                'stud_email' =>"-",
                'stud_phno' =>null,
                'stud_type' =>"-",
                'stud_father' => "-",
                'stud_mother' => "-",
                'stud_id' => $row[1],
                'stud_status' => "active",
            ]);

                $addstudent->save();

                $addstudent->user()->create(
                    ['name' => $row[0],
                'username' => $row[1],
                'user_type' => "student",
                'password' =>  $row[2],
                'status' => "active"
            ]
                );
            }
        }

    }


i added this line if ($row->filter()->isNotEmpty()) and it worked for me thank you guys

1 like

Please or to participate in this conversation.