Do you have a SpreadsheetController class in the Controllers directory?
// app/Http/Controllers/SpreadsheetController.php
<?php
namespace App\Http\Controllers;
class SpreadsheetController
{
// ...
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Im learning by following a guide how to import excel into database and show records inserted. I have this as my route:
Do you have a SpreadsheetController class in the Controllers directory?
// app/Http/Controllers/SpreadsheetController.php
<?php
namespace App\Http\Controllers;
class SpreadsheetController
{
// ...
@tykus yes
@Innobas share your code.
@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"));
}
}
@Innobas can you also share the autoload part of your composer.json file?
@tykus "autoload": { "psr-4": { "App\": "app/", "Database\Factories\": "database/factories/", "Database\Seeders\": "database/seeders/" } },
@Innobas nothing seems amiss; can you try running composer dump from the command line, although I don;t expect this should affect the result?
@tykuslet me try asap
If you have renamed the file or class name, you might need to run composer dump
@Snapey haven't renamed anything
Can you show the line from your routes file?
@Snapey Route::get('/spreadsheet/import','SpreadsheetController@import');
Route::post('/spreadsheet/import','SpreadsheetController@import');
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?
So what folder is your SpreadsheetController.php actually in?
@Snapey C:\xampp\htdocs\test\app\Http\Controllers\SpreadsheetController
@Innobas C:\xampp\htdocs\test\app\Http\Controllers\SpreadsheetController.php?
@tykus yes thats the full path. the folder name is Controllers. the controller route is defined like this: use App\Http\Controllers\SpreadsheetController;
@Innobas do a screenshot of the folder structure?
@Snapey how do i attach a picture? i cant see a provision
Hi! have solved the issue. the problem was with the controller file name.. it had no extension so have just added .php
@Innobas (slaps forehead)
Please mark it solved
@Snapey how do i mark it solved
@Innobas I literally asked you this question... and you said you had the correct path 🤦♂️
@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.