在我的 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') 效果很好。
這是為什麼呢?我錯過了什麼嗎?
您很可能從區分大小寫的檔案系統載入類,這就是這個問題導致問題的原因。雖然 PHP 類別名稱不區分大小寫。正如它們在聲明中出現的那樣,這被認為是良好的做法
您需要更改此行:
至