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

noblemfd's avatar

How to use Maatwebsite Excel import in Laravel Restful API

Initially I have full Laravel Application. In it I use Maatwebsite-3.1 to import from Excel into the database as shown below:

class GradesImport implements
    ToCollection,
    WithHeadingRow,
    SkipsOnError,
    WithValidation,
    SkipsOnFailure
{
    use Importable, SkipsErrors, SkipsFailures;//, SkipsFailures, SkipsErrors; 

    public function collection(Collection $rows)
    {            
        foreach ($rows as $row) 
        {
            $this->gradelevel = $row['grade_level_code'];         
            HrEmployee::where('company_id', auth()->user()->company_id)
                ->where('employee_code', $row['staff_id'])
                ->where('email', $row['official_email'])     
                ->update([
                    'grade_level_id' => $this->getGradeLevel(),
                ]);
        }
    } 
    
    public function rules(): array
    {
        return [
            '*.employee_code' => [
                'required',
                'min:2',
                'max:20',               
            ], 
            '*first_name' => [
                'required',
                'string',
                'min:3',
                'max:50'               
            ],  
            '*last_name' => [
                'required',
                'string',
                'min:3',
                'max:50'               
            ],            
            '*grade_level_code'            => 'required',  
            '*official_email' => [
                'required',
                'email', // official email
                'max:100',               
            ],            
        ];    
    } 
    
    public function customValidationAttributes()
    {
        return [
            'company_id'                                => 'Company',
            'grade_level_code'                          => 'Grade Level Code',
            'employee_code'                             => 'Staff ID',
            'first_name'                                => 'First Name',
            'last_name'                                 => 'Last Name',            
        ];
    }
    
    public function getGradeLevel(){
        if(!empty($this->gradelevel) || !$this->gradelevel){

            return Grade::where('grade_level_code',$this->gradelevel)->where('company_id',Auth::user()->company_id)->pluck('id')->first();
        } else {
            return 0;
        }
    }  
    
    public function headingRow(): int
    {
        return 1;
    }         
}

The above code is working fine in plain excel.

But Now I'm to convert it to Laravel Restful API and then use Angular-11 as the frontend.

How do I achieve this with the same code? Or is there any better way?

Thanks

0 likes
1 reply
Snapey's avatar

But Now I'm to convert it to Laravel Restful API and then use Angular-11 as the frontend.

frontend...

nothing to do with the processing of the file which happens in PHP on your server. You still need to get the file to the server for processing.

Please or to participate in this conversation.