Cristian Stoenica
Cristian Stoenica

Reputation: 181

Class 'App\Models\Student' not found when trying to acces model

i'm new to Laravel and i'm trying to use a model but i get this error, what could be? Laravel Framework 8.34.0 PHP 7.4.3

I cant access this class App\Models\Student

Here's my controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Student;


class ApiController extends Controller
{

    public function createStudent(Request $request) {
        $student = new Student;
        $student->name = $request->name;
        $student->course = $request->course;
        $student->save();

        return response()->json([
          "message" => "student record created"
        ], 201);
      }

    
}

Ande Here's the model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    protected $table = 'students';

    protected $fillable = ['name', 'course'];
}

Upvotes: 0

Views: 7475

Answers (2)

hide and Seek
hide and Seek

Reputation: 1

Lets Assume that you are using Laravel 8.Then you need to add use App\Models\Student; to your Controller. And change namespace App; To namespace App\Models;

The App\Models\Student. Is the location of your Student model ,which means the model Student should be inside Models folder inside the app folder of your laraval 8 project. Make sure if the Student model is really existing...

If you are another version (below laravel8) then the path will be App\Student

Upvotes: 0

dev_mustafa
dev_mustafa

Reputation: 1111

Here is what you can do:

Change in model

namespace App\Models;

Change in Controller

use App\Models\Student;

Also, try to run composer dump-autoload

Upvotes: 3

Related Questions