Home  >  Q&A  >  body text

Laravel error in production environment: "App\Models\review" class not found

In my Laravel application I have 3 models: Movies, users, comments

Comment:

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

Movie:

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

user:

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

Invalid query

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

In localhost it works fine. But after deploying in digitalOcean it returns "Class "AppModelsreview" not found"

I tried the console on 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.

But after running this command in the same session:

Review::all()

Previous Movie::has('review') worked well.

Why is this? Did I miss something?

P粉029057928P粉029057928270 days ago354

reply all(1)I'll reply

  • P粉124070451

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

    You are most likely loading the class from a case-sensitive file system, which is why this issue is causing the problem. Although PHP class names are not case sensitive. This is considered good practice as they appear in the statement

    You need to change this line:

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

    to

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

    reply
    0
  • Cancelreply