简体   繁体   English

Laravel - 找不到模型类

[英]Laravel - Model Class not found

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 - 使用命令php artisan make:model创建模型
- Tried to get all entries from table posts with echo Post::all() - 尝试使用echo Post::all()从表格posts中获取所有条目

I used the following code:我使用了以下代码:

router.php路由器.php

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

Post.php Post.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 模型类

Laravel 5 promotes the use of namespaces for things like Models and Controllers. Laravel 5 提倡在模型和控制器等事物中使用命名空间 Your Model is under the App namespace, so your code needs to call it like this:您的模型位于App命名空间下,因此您的代码需要像这样调用它:

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

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

As mentioned in the comments you can also use or 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

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-autoload and it didn't help me.我什至尝试了composer dump-autoload并没有帮助我。

Surprisingly (to me) I then did composer dump-autoload -o which according to Composer's help, "optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production."令人惊讶的是(对我来说)然后我做了composer dump-autoload -o根据 Composer 的帮助,“优化 PSR0 和 PSR4 包以加载类映射,这对生产有好处。” Somehow doing it that way got composer to behave and include the class correctly in the autoload_classmap.php file.不知何故,这样做让作曲家在 autoload_classmap.php 文件中正确地表现并包含该类。

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 App folder, 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

I had this same issue but it just turns out that composer creates a separate Models folder for Laravel 8.*, so instead of我有同样的问题,但事实证明作曲家为 Laravel 8.* 创建了一个单独的模型文件夹,所以而不是

use App\Post;

I had to write:我不得不写:

use App\Models\Post;

In your router.php file, you should use the model class like this在你的router.php文件中,你应该像这样使用模型类

 use App\Models\Post;

and use the model class like this.并像这样使用模型类。

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

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

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\Post in this case).我建议您开始使用 IDE,它会建议您导入所有必需的命名空间(在本例中为\App\Post )。

If after changing the namespace and the config/auth.php it still fails, you could try the following:如果更改命名空间和config/auth.php后仍然失败,您可以尝试以下操作:

  1. In the file vendor/composer/autoload_classmap.php change the line App\\User' => $baseDir . '/app/User.php',在文件vendor/composer/autoload_classmap.php中更改行App\\User' => $baseDir . '/app/User.php', App\\User' => $baseDir . '/app/User.php', , to App\\Models\\User' => $baseDir . '/app/Models/User.php', App\\User' => $baseDir . '/app/User.php', , 到App\\Models\\User' => $baseDir . '/app/Models/User.php', App\\Models\\User' => $baseDir . '/app/Models/User.php',

  2. At the beginning of the file app/Services/Registrar.php change "use App\User" to "App\Models\User"在文件app/Services/Registrar.php的开头将“use App\User”更改为“App\Models\User”

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 Model line.您需要密切注意class Post extend Model行。 The class Post defined here will be your namespace for your controller.此处定义的Post类将是您的控制器的命名空间。

For me it was really silly I had created a ModelClass file without the .php extension.对我来说,创建一个没有 .php 扩展名的 ModelClass 文件真的很愚蠢。 So calling that was giving model not found.所以打电话给没有找到模型。 So check the extension has .php所以检查扩展名有 .php

I had the same issue.我遇到过同样的问题。 It turned out my Post model was in a php file whose name was not Post.php原来我的 Post 模型在一个名称不是 Post.php 的 php 文件中

=> the filename must match the name of your class (Post class in Post.php file) => 文件名必须与你的类名相匹配(Post.php 文件中的 Post 类)

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

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