简体   繁体   English

Laravel “在生产中找不到类 \”App\\Models\\review\“

[英]Laravel "Class \"App\\Models\\review\" not found" in production

In my Laravel app there are 3 Models: Movie, User, Review在我的 Laravel 应用程序中有 3 个模型:电影、用户、评论

Review :回顾

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Review extends Model
{
    use HasFactory;

    protected $hidden = ['user_id','movie_id','created_at','updated_at'];

    public function movie(){
        return $this->belongsTo(Movie::class);
    }

    public function user(){
        return $this->belongsTo(User::class);
    }
}

Movie :电影

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Movie extends Model
{
    use HasFactory;

    protected $hidden = ['created_at','updated_at'];

    public function review(){
        return $this->hasMany(review::class);
    }

    public function getAvg()
    {
        return $this->review()->average('rating');
    }

    public function count()
    {
        return $this->review()->count();
    }

    public function getBestRating()
    {
        return $this->review()->max('rating');
    }

    public function getWorstRating()
    {
        return $this->review()->min('rating');
    }
}

User :用户

<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use HasApiTokens, HasFactory, Notifiable;

    public function review()
    {
        return $this->hasMany(Review::class);
    }
}

The query that doesn't work无效的查询

$movies = Movie::has('review')->with('review.user')->get();

In localhost it works fine.在本地主机上它工作正常。 but after deploying in digitalOcean it returns "Class "App\Models\review" not found"但在 digitalOcean 中部署后,它返回“未找到类“App\Models\review””

I tried the console on digitalOcean:我在 digitalOcean 上尝试了控制台:

> Movie::has('review')->get()
[!] Aliasing 'Movie' to 'App\Models\Movie' for this Tinker session.

 ERROR Class "App\Models\review" not found in vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php on line 775.

but after running this in the same session:但在同一个 session 中运行之后:

Review::all()

the previous Movie::has('review') works fine.之前的 Movie::has('review') 工作正常。

Why is that?这是为什么? Am I missing something?我错过了什么吗?

Change this part of the movie model like this:像这样更改movie model 的这一部分:

public function review(){
    return $this->hasMany(Review::class);
}

Note the uppercase R in Review .注意Review中的大写R

In PHP, class names are case-sensitive, so review and Review are considered two different classes. PHP、class名字是区分大小写的,所以review和Review被认为是两个不同的类。 Whilst it might be ignored in development, in production it will not.虽然它可能在开发中被忽略,但在生产中它不会。

You need to change this line:您需要更改此行:

return $this->hasMany(review::class);

to

return $this->hasMany(Review::class);

You are using different letter case.您正在使用不同的字母大小写。 Class is defined as Review but you invoke review : Class 定义为Review但您调用review

$this->hasMany(review::class);

Unlike variable names, neither class names nor function names are case sensitive in PHP:与变量名不同,class 名称和 function 名称在 PHP 中都不区分大小写:

class Example
{
    public static function test(): void
    {
        echo 'Hello, World!';
    }
}

// This works
eXaMpLe::TeSt();

// This doesn't: Warning: Undefined variable $FOO
$foo = 1;
echo $FOO;

Demo演示

However, Laravel is using a PSR-4 compliant class auto-loader to locate and read the PHP files where classes are defined.但是,Laravel 使用符合PSR-4的 class 自动加载器来定位和读取定义类的 PHP 文件。 Such auto-loader maps class names to file names, so whenever App\Models\Review is used in code, a filesystem path like /your/project/libraries/path/App/Models/Review.php is loaded.这样的自动加载器将 class 名称映射到文件名,因此每当在代码中使用App\Models\Review时,都会加载类似/your/project/libraries/path/App/Models/Review.php的文件系统路径。 If any of your path components, either a directory or the file name, was created using a different case, whether it'll work or not will depend on the case-sensitive flag in the file system.如果您的任何路径组件(目录或文件名)是使用不同的大小写创建的,那么它是否有效将取决于文件系统中区分大小写的标志。 If your file system is something like NTFS (typical on Windows) it'll decide that review.php and Review.php are the same file, so it'll work.如果您的文件系统类似于 NTFS(通常在 Windows 上),它将确定review.phpReview.php是同一个文件,因此它会起作用。 In most Linux filesystems, it's actually allowed to have two different review.php and Review.php files, so they need to be considered different.在大多数 Linux 文件系统中,实际上允许有两个不同的review.phpReview.php文件,因此需要将它们视为不同的。

It's also worth nothing that the ::class syntax is just a string feature and doesn't really trigger class autoloading: ::class语法只是一个字符串功能,并没有真正触发 class 自动加载,这也是毫无价值的:

// This is fine
echo Does\Not\Exist::class;

So you don't get the error right in the offending line, but later down the line when the class is eventually used.所以你不会在有问题的行中得到正确的错误,但后来当最终使用 class 时,错误就出现了。

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

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