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?
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:
to