php Laravel - 找不到模型类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28350232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:48:49  来源:igfitidea点击:

Laravel - Model Class not found

phplaravel

提问by Sylnois

When starting to work with models I got the following error

开始使用模型时,出现以下错误

Class Post not found`.

找不到班级帖子`。

All I did:
- Created a Model with the command php artisan make:model
- Tried to get all entries from table postswith echo Post::all()

我所做的:
-创建一个模型命令php artisan make:model
-试图让所有条目从表postsecho Post::all()

I used the following code:

我使用了以下代码:

router.php

路由器文件

Route::get('/posts', function(){
    $results = Post::all();
    return $results;
});

Post.php

后.php

<?php 
namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model {
    protected $table = 'posts';    
}

What I tried
- Renaming Class
- Dump-autoload (Laravel 4 Model class not found)

我尝试过的
- 重命名类
- 转储自动加载(未找到 Laravel 4 模型类

回答by Dan Smith

Laravel 5 promotes the use of namespacesfor things like Models and Controllers. Your Model is under the Appnamespace, so your code needs to call it like this:

Laravel 5 促进了命名空间的使用,比如模型和控制器。您的模型位于App命名空间下,因此您的代码需要像这样调用它:

Route::get('/posts', function(){

        $results = \App\Post::all();
        return $results;
});

As mentioned in the comments you can also useor import a namespace in to a file so you don't need to quote the full path, like this:

正如评论中提到的,您还use可以将命名空间导入文件中,这样您就不需要引用完整路径,如下所示:

use App\Post;

Route::get('/posts', function(){

        $results = Post::all();
        return $results;
});

While I'm doing a short primer on namespaces I might as well mention the ability to alias a class as well. Doing this means you can essentially rename your class just in the scope of one file, like this:

虽然我正在做一个关于命名空间的简短入门,但我也不妨提一下为类设置别名的能力。这样做意味着您基本上可以在一个文件的范围内重命名您的类,如下所示:

use App\Post as PostModel;

Route::get('/posts', function(){

        $results = PostModel::all();
        return $results;
});

More info on importing and aliasing namespaces here: http://php.net/manual/en/language.namespaces.importing.php

有关在此处导入和别名命名空间的更多信息:http: //php.net/manual/en/language.namespaces.importing.php

回答by jlbang

I was having the same "Class [class name] not found" error message, but it wasn't a namespace issue. All my namespaces were set up correctly. I even tried composer dump-autoloadand it didn't help me.

我遇到了相同的“找不到类 [类名]”错误消息,但这不是命名空间问题。我所有的命名空间都设置正确。我什至尝试过composer dump-autoload,但对我没有帮助。

Surprisingly (to me) I then did composer dump-autoload -owhich according to Composer's help, "optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production." Somehow doing it that way got composer to behave and include the class correctly in the autoload_classmap.php file.

令人惊讶的是(对我而言)然后我composer dump-autoload -o根据 Composer 的帮助做了这件事,“优化了 PSR0 和 PSR4 包以加载类映射,这对生产有好处。” 以某种方式这样做使作曲家能够正确地将类包含在 autoload_classmap.php 文件中。

回答by Wei

I had the same error in Laravel 5.2, turns out the namespace is incorrect in the model class definition.

我在 Laravel 5.2 中遇到了同样的错误,结果模型类定义中的命名空间不正确。

I created my model using the command:

我使用以下命令创建了我的模型:

php artisan make:model myModel

By default, Laravel 5 creates the model under Appfolder, but if you were to move the model to another folder like I did, you must change the the namespace inside the model definition too:

默认情况下,Laravel 5 在App文件夹下创建模型,但是如果您像我一样将模型移动到另一个文件夹,您也必须更改模型定义中的命名空间:

namespace App\ModelFolder;

To include the folder name when creating the model you could use (don't forget to use double back slashes):

在创建您可以使用的模型时包含文件夹名称(不要忘记使用双反斜杠):

php artisan make:model ModelFolder\myModel

回答by Sheetal Mehra

In your router.phpfile, you should use the model class like this

在您的router.php文件中,您应该像这样使用模型类

 use App\Post;

and use the model class like this.

并像这样使用模型类。

Route::get('/posts', function() {

        $results = Post::all();
        return $results; });

回答by jpbourbon

If after changing the namespace and the config/auth.phpit still fails, you could try the following:

如果更改命名空间后config/auth.php仍然失败,您可以尝试以下操作:

  1. In the file vendor/composer/autoload_classmap.phpchange the line App\\User' => $baseDir . '/app/User.php',, to App\\Models\\User' => $baseDir . '/app/Models/User.php',

  2. At the beginning of the file app/Services/Registrar.phpchange "use App\User" to "App\Models\User"

  1. 在文件中,vendor/composer/autoload_classmap.php将行更改 App\\User' => $baseDir . '/app/User.php',App\\Models\\User' => $baseDir . '/app/Models/User.php',

  2. 在文件的开头app/Services/Registrar.php将“use App\User”更改为“App\Models\User”

回答by Dmitry

As @bulk said, it uses namespaces.

正如@bulk 所说,它使用命名空间。

I recommend you to start using an IDE, it will suggest you to import all the required namespaces (\App\Postin this case).

我建议您开始使用 IDE,它会建议您导入所有必需的命名空间(\App\Post在本例中)。

回答by DragonFire

For me it was really silly I had created a ModelClass file without the .php extension. So calling that was giving model not found. So check the extension has .php

对我来说,我创建了一个没有 .php 扩展名的 ModelClass 文件真的很愚蠢。所以打电话给找不到模型。所以检查扩展名是否有 .php

回答by Erich Meissner

Make sure to be careful when editing your model file. For example, in your post model:

编辑模型文件时一定要小心。例如,在您的帖子模型中:

<?php 
namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model {
    protected $table = 'posts';    
}

You need to pay close attention to the class Post extend Modelline. The class Postdefined here will be your namespace for your controller.

您需要密切注意class Post extend Model线路。Post此处定义的类将是您的控制器的命名空间。