简体   繁体   English

Laravel - 找不到类/模型

[英]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: AdminController代码是:

<?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 .我的 model 类别位于App/Models/Category.php

What I've tried:我试过的:

  1. Change from use App\Category;use App\Category; to use Category , to use \Category , to use App\Models\Category . use Categoryuse \Categoryuse App\Models\Category
  2. composer dump-autoload . 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. 由于Laravel使用PSR-4自动加载功能 ,因此您需要确保名称空间与真实目录结构匹配。

You say that your Category model is located at App/Models/Category.php so its namespace should be App\\Models . 您说Category模型位于App/Models/Category.php因此其名称空间应为App\\Models Then, in your controllers you would use App\\Models\\Category . 然后,在控制器中,您将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: 如果您在AdminController控制器中将此代码用于Category Model:

use App\Models\Category;

then your Category model itself has this namespace 那么您的Category模型本身就有这个名称空间

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. 当在Controllers或Seeders中调用模型时,我也遇到了相同的错误。

The best solution is to import your Model inside the Controllers. 最好的解决方案是将模型导入控制器中。

  • Add the below line at the top of your AdminController. 将以下行添加到AdminController的顶部。

    use App\\Models\\Category

This is applicable for all Classes where you try to call your models. 这适用于您尝试调用模型的所有类。

use App\Models\Category使用 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; 1.导入namespace App\Http\Controllers;

2,Check Controller Letter is in Upper Or LowerCase 2、检查Controller字母是大写还是小写
like this像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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