0

I am getting this error "Class 'App\Models\Student' not found".

Please check my codes:

      <?php

          namespace App\Http\Controllers;

          use Illuminate\Http\Request;
           use App\Models\Student;
           class StudentController extends Controller
           {
              public function data(){


                   $stud = new Student;
                   $stud->name = 'Mona Lisa';
                   $stud->rollnumber = '001';
                   $stud->save();


             }

}

6
  • Can you show the code for your Student class? Commented Dec 10, 2018 at 11:19
  • Yeah... <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Student extends Model { protected $connection = 'mysql'; protected $primaryKey = 'id'; protected $table = 'students'; protected $fillable = array( 'id', 'name', 'rollnumber', ); public $timestamps = false; } Commented Dec 10, 2018 at 11:21
  • Where is the Student class file located? Commented Dec 10, 2018 at 11:22
  • Student class is Student.php model Commented Dec 10, 2018 at 11:29
  • @JnanBikashDeka Did you put your model class in a new directory? By default they don't have their own directory. They lie in the app directory with many other directories. Commented Dec 10, 2018 at 11:54

5 Answers 5

2

Your error is most likely in the namespace declaration of the Student model.

check in your class file App\Models\Student.php that the namespace is correct.

namespace App\Models;

then try to run in the console:

composer dump-autoload
Sign up to request clarification or add additional context in comments.

Comments

0

Change this line:

use App\Models\Student;

to:

use App\Student;

Comments

0

Your error suggests that class is not loaded into the controller. I am guessing from the issue that you have created a separate directory for models if you have then you need to update your composer file to include new class paths for model classes. locate autoload in you composer.json file and add an entery in classmap array as shown below

"autoload": {
    "classmap": [
        "database",
        "app/models"
    ]
}

then on command line navigate to your project directory and run the following command:

composer dump-autoload

and if you didn't create new models directory then just run

composer dump-autoload

Comments

0

Make sure your namespace of student model

use App\Models\Student If your model is in App\Models directory use App\Student if your model is in app directory

Finally run composer dump-autoload

Comments

0

check your namespace of student model

use App\Models\Student If your model is in use App\Models directory use App\Student if your model is in app directory

then go to command line and run composer dump-autoload

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.