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

Innobas's avatar

Target class [App\Http\Controllers\SpreadsheetController] does not exist.

Im learning by following a guide how to import excel into database and show records inserted. I have this as my route:

0 likes
32 replies
tykus's avatar

Do you have a SpreadsheetController class in the Controllers directory?

// app/Http/Controllers/SpreadsheetController.php
<?php

namespace App\Http\Controllers;

class SpreadsheetController 
{
    // ...
Innobas's avatar

@tykus <?php namespace App\Http\Controllers;

use Illuminate\Http\Request; use Illuminate\Routing\Controller as BaseController;

class SpreadsheetController extends BaseController { function import(Request $request){ $title = "Import Spreadsheet"; $template = url('documents/template-users.xlsx'); if($_POST){ $request->validate([ 'file1' => 'required|mimes:xlsx|max:10000' ]); $file = $request->file('file1'); $name = time().'.xlsx'; $path = public_path('documents'.DIRECTORY_SEPARATOR);

        if ( $file->move($path, $name) ){
            $inputFileName = $path.$name;
            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
            $reader->setReadDataOnly(true);
            $reader->setLoadSheetsOnly(["USER DATA"]);
            $spreadSheet = $reader->load($inputFileName);
            $workSheet = $spreadSheet->getActiveSheet();
            $startRow = 2;
            $max = 3000;
            $columns = [
                "A"=>"id",
                "B"=>"name",
                "C"=>"email",
                "D"=>"address"
            ];
            $data_insert = [];
            for($i=$startRow; $i<$max; $i++){
                $id = $workSheet->getCell("A$i")->getValue();
                if(empty( $id)||!is_numeric( $id ))continue;

                $data_row = [];
                foreach ($columns as $col=>$field) {
                    $val = $workSheet->getCell("$col$i")->getValue();
                    $data_row[$field] = $val;
                }
                $data_insert[] = $data_row;
            }
            \DB::table('users')->truncate();
            \DB::table('users')->insert($data_insert);

            return redirect('spreadsheet/import')->with('success', 'Data imported successfully!');
        }
    }

    return view("spreadsheet.import", compact("title", "template"));
}

}

tykus's avatar

@Innobas can you also share the autoload part of your composer.json file?

Innobas's avatar

@tykus "autoload": { "psr-4": { "App\": "app/", "Database\Factories\": "database/factories/", "Database\Seeders\": "database/seeders/" } },

tykus's avatar

@Innobas nothing seems amiss; can you try running composer dump from the command line, although I don;t expect this should affect the result?

1 like
Snapey's avatar

If you have renamed the file or class name, you might need to run composer dump

1 like
tykus's avatar

@Innobas it is really hard to know in that case; everything we have suggested above would be typical causes for this kind of error

Snapey's avatar

Can you show the line from your routes file?

Innobas's avatar

@Snapey Route::get('/spreadsheet/import','SpreadsheetController@import');

Route::post('/spreadsheet/import','SpreadsheetController@import');

Innobas's avatar

So, i'm trying to import and export content into and from the msql databse. is there any guide you can recommend i go through?

Innobas's avatar

@tykus its very much related in that im trying to import content into the datase and in the process im getting that error.... i know the main isue here is about the Target class not existing.

Snapey's avatar

So what folder is your SpreadsheetController.php actually in?

Innobas's avatar

@Snapey C:\xampp\htdocs\test\app\Http\Controllers\SpreadsheetController

tykus's avatar

@Innobas C:\xampp\htdocs\test\app\Http\Controllers\SpreadsheetController.php?

Innobas's avatar

@tykus yes thats the full path. the folder name is Controllers. the controller route is defined like this: use App\Http\Controllers\SpreadsheetController;

Innobas's avatar

Hi! have solved the issue. the problem was with the controller file name.. it had no extension so have just added .php

tykus's avatar

@Innobas I literally asked you this question... and you said you had the correct path 🤦‍♂️

Snapey's avatar

@Innobas hover over one of the replies that you think helped the most and a button will appear.

Please or to participate in this conversation.