In my Laravel app there are 3 Models: Movie, User, Review
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"
I tried the console on 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:
Review::all()
the previous Movie::has('review') works fine.
Why is that? Am I missing something?
CodePudding user response:
Change this part of the movie
model like this:
public function review(){
return $this->hasMany(Review::class);
}
Note the uppercase R
in Review
.