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

vidhyaprakash85's avatar

Laravel Excel upload file from request and check email fired

I have laravel excel import test which first check whether columns are matched or not. If columns are not matched it will send an email to the user. I need to test this first.

test('superadmin can able to upload wrong file', function () {
    $this->withoutExceptionHandling();
    $branches = Branch::select('id', 'name')->get();
    $response = $this->get(route('user.create'));
    $response->assertSee("Teacher User Import");
    Excel::fake();
    Storage::fake();

    $file = UploadedFile::fake()->create(
        base_path('tests/Files/teacher-wrong-file.xlsx'),
        'teacher-wrong-file.xlsx'
    );
    $data = [
        'teacherbranch' => $branches->first()->id,
        'teacherimportfile' => $file,
        "submit" => 'TeacherImport',
    ];
    $response = $this->post(route('user.store'), $data);
    $response->assertStatus(302);
    $response->assertRedirect(route('user.index'));
});

I have getting the error message

Undefined array key 0

  at app/Services/TeacherImportService.php:35
     31▕     private function validateExcelColumnNames($file): bool
     32▕     {
     33▕         // Use the Excel facade to check column names
     34▕         $columnNames = (new HeadingRowImport)->toArray($file);
  ➜  35▕         $convertedColumnNames = (array_values($columnNames[0][0]));
     36▕ 
     37▕         // Define the expected column names
     38▕         $expectedColumns = ['lecturer_code', 'lecturer_name', 'lecturer_status', 'position_code', 'position_name', 'lecturer_category', 'mininmum_teaching_hours', 'maximum_teaching_hours', 'department_name', 'section_name'];
     39▕ 

Please help in uploading file issue and the sending email.

0 likes
6 replies
vidhyaprakash85's avatar

@tisuchi Here is the code

class TeacherImportService
{
    public function import(Request $request)
    {
        if ($this->validateExcelColumnNames($request->teacherimportfile)) {
            Excel::queueImport(new UserTeacherImport($request->teacherbranch, Auth::user()), $request->teacherimportfile);
        } else {
            if (isset(Auth::user()->email)) {
                Mail::to(Auth::user()->email)->queue(new IncorrectColumnsMail());
                Log::error("Teacher Import Failed. Incorrect Columns - " . get_branch_name_from_id(session('branch_id')));
            }
        }
    }

    private function validateExcelColumnNames($file): bool
    {
        // Use the Excel facade to check column names
        $columnNames = (new HeadingRowImport)->toArray($file);
        $convertedColumnNames = (array_values($columnNames[0][0]));

        // Define the expected column names
        $expectedColumns = ['lecturer_code', 'lecturer_name', 'lecturer_status', 'position_code', 'position_name', 'lecturer_category', 'mininmum_teaching_hours', 'maximum_teaching_hours', 'department_name', 'section_name'];

        // Compare the expected and actual column names
        $diff = array_diff($expectedColumns, $convertedColumnNames);

        // If there are differences, handle the validation error
        if (!empty($diff)) {
            return false;
        }
        return true;
    }
tisuchi's avatar

@vidhyaprakash85 Wrapping your code by if-else should solve the issue.


private function validateExcelColumnNames($file): bool
{
    // Use the Excel facade to check column names
    $columnNames = (new HeadingRowImport)->toArray($file);

    if (isset($columnNames[0]) && isset($columnNames[0][0])) {
        $convertedColumnNames = (array_values($columnNames[0][0]));

        // Define the expected column names
        $expectedColumns = ['lecturer_code', 'lecturer_name', 'lecturer_status', 'position_code', 'position_name', 'lecturer_category', 'mininmum_teaching_hours', 'maximum_teaching_hours', 'department_name', 'section_name'];

        // Compare the expected and actual column names
        $diff = array_diff($expectedColumns, $convertedColumnNames);

        // If there are differences, handle the validation error
        return empty($diff);
    } else {
        // Log an error or handle it as needed
        // For example, throw an exception or return false
        return false;
    }
}

Please or to participate in this conversation.