首页  >  问答  >  正文

生产环境中出现Laravel错误:找不到“App\Models\review”类

在我的 Laravel 应用程序中有 3 个模型: 电影、用户、评论

评论

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

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);
    }
}

电影

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

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');
    }
}

用户

<?php

namespace AppModels;

// use IlluminateContractsAuthMust VerifyEmail;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
use LaravelSanctumHasApiTokens;
use TymonJWTAuthContractsJWTSubject;

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

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

无效的查询

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

在本地主机中它工作正常。但是在 digitalOcean 中部署后它返回“找不到类“AppModelsreview””

我在 digitalOcean 上尝试了控制台:

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

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

但是在同一会话中运行此命令后:

Review::all()

之前的 Movie::has('review') 效果很好。

这是为什么呢?我错过了什么吗?

P粉029057928P粉029057928319 天前402

全部回复(1)我来回复

  • P粉124070451

    P粉1240704512023-12-29 00:44:04

    您很可能从区分大小写的文件系统加载类,这就是这个问题导致问题的原因。虽然 PHP 类名不区分大小写。正如它们在声明中出现的那样,这被认为是良好的做法

    您需要更改此行:

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

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

    回复
    0
  • 取消回复