Laravel - 未找到类/型号

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.

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.

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

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.

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.