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

inayan's avatar

Again getting class not found error while loading a page in laravel8

Hi I am getting class 'App\Http\Controllers\ Student' not found error when loading localhost:8000/students . Tried to rename classes every way but error exist still . tried composer dump-autoload but didn't work . Snippet of my StudentController.php code is below =

<?php


namespace App\Http\Controllers;
use App\Models\Student;
use Illuminate\Http\Request;




class StudentController extends Controller
{
   public function index()
   {
    $student = Student::all();
        return view('student.index', compact('student'));
}

My Student.php file code below -


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    use HasFactory;
    protected $table = 'students';
    protected $fillable = [
         'name',
         'email',
         'course',
         'country',
         'profile_image',
    ];
}

My web.php code is below =

<?php
use App\Http\Controllers\StudentController;
use Illuminate\Support\Facades\Route;

// New era student profile details

Route::get('/students', [StudentController::class, 'index']);
Route::get('/add-student', [StudentController::class, 'create']);
Route::post('/add-student', [StudentController::class, 'store']);

0 likes
36 replies
Snapey's avatar

All looks fine. You are sure all these files are saved?

1 like
inayan's avatar

@Snapey Yes these are all saved files . I am stuck here since yesterday .

axeloz's avatar

Also are you sure the Student.php model class exists in the proper path: app/Models/Student.php (case matters)

1 like
vincent15000's avatar

Where did you save the Student model file ? In App\Models\Student ? Or in another folder ?

Sinnbeck's avatar

Are you sure you are showing the correct controller file? The error should tell you the file

1 like
inayan's avatar

@vincent15000 the error is Class 'App\Http\Controllers\ Student' not found and highlights the line $student = Student::all();

Snapey's avatar

@axeloz correct, in the error message, but I'm puzzled how this can happen unless there is a non-printing space somewhere

Class 'App\Http\Controllers\ Student' not found

@inayan I would delete the entire line Student::all() and retype it from scratch

Snapey's avatar

@inayan The error must be restricted to the controller file, because it is looking in controllers namespace for the Student model instead of obeying your use statement

axeloz's avatar

@inayan

In your controller, delete this entire line : use App\Models\Student; and retype it with extra care

axeloz's avatar

@inayan Your file is screwed. UTF8-bom, wrong charset or else you have very big fingers. But something nasty happened

Your editor should be set for UTF8-no bom, Unix style returns

axeloz's avatar
axeloz
Best Answer
Level 2

@inayan to explain what's going on, you have non-breaking spaces instead of space. So the space in return view(...) is not considered as a space. You should set your code editor to highlight special chars.

Here is what my code editor shows me when I accidently used non-breaking spaces : Capture-d-e-cran-2022-07-03-a-19-23-14.png

1 like
inayan's avatar

@axeloz okay that could be the case . But I am not seeing anything like that , I am using visual studio code .

axeloz's avatar

@inayan I'm using Visual Studio as well

editor.unicodeHighlight.invisibleCharacters

inayan's avatar

@axeloz I found an extension called 'gremlins' . may be thats the one i need .

Snapey's avatar

did you type this code or copy and paste it?

1 like
Snapey's avatar

@inayan just humour me.. Write out the same function below it, then delete the original

1 like
inayan's avatar

@axeloz I am just learning to code bro so need to do that a little .

Please or to participate in this conversation.