简体   繁体   中英

Laravel - Class/Model not found

Initially, this code worked, but it doesn't work anymore. I don't know what is causing the issue.

The error is:

FatalErrorException in AdminController.php line 64:
Class 'App\Category' not found

AdminController code is:

<?php

namespace App\Http\Controllers;

use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Category;
use View;   

class AdminController extends Controller
{
    public function article_get()
    {
        $categories = Category::latest()->get();
        return View::make('create.article')->with('categories',$categories);
    }
}

My model Category is located at App/Models/Category.php .

What I've tried:

  1. Change from use App\Category; to use Category , to use \Category , to use App\Models\Category .
  2. composer dump-autoload .

A few hours ago I had a working project, but now my models are not found.

Because Laravel uses PSR-4 autoloading , you need to make sure your namespaces match the real directory structure.

You say that your Category model is located at App/Models/Category.php so its namespace should be App\\Models . Then, in your controllers you would use App\\Models\\Category .

Your code seems fine. May be it has an issue with namespace in Category model.

if you use this code for Category Model in your AdminController controller:

use App\Models\Category;

then your Category model itself has this namespace

namespace App\Models;

Check if your model namespace is still there or not.

Thanks

Do it like this and it must work:

use App\Models\Category

and make sure that the model Category is inside a folder named Models.

I had also faced the same error when Models are called inside Controllers or Seeders.

The best solution is to import your Model inside the Controllers.

  • Add the below line at the top of your AdminController.

    use App\\Models\\Category

This is applicable for all Classes where you try to call your models.

use App\Models\Category

//check your models is capital or small letter sometimes issue like that is a big problem

App\Models\Category

PLease Check This twoThings

1.Importing namespace App\Http\Controllers;

2,Check Controller Letter is in Upper Or LowerCase
like this

use App\models\Category----->❌❌❌
use App\Models\Category----->✅✅✅

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM