Hello I’m new to Laravel and Eloquent, all I found about this problems it that it can by Case sensitivity problems but I can’t find any case problems.
Class "AppModelsTvEpisode" not found
I’m trying to get UserTvEpisode list to view it works fine in localhost (using Windows), but not on production server in throws an error that the class TvEpisode is not found.
User has many UserTvEpisode ‘s . TvEpisode has many UserTvEpisode ‘s .
Filenames: TvEpisode.php , UserTvEpisode.php, TvEpisodeController.php
Controller that tries to get list:
<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUserTvEpisode; use AppModelsTvEpisode; use Auth; class TvEpisodeController extends Controller { /** * Display a listing of the resource. * * @return IlluminateHttpResponse */ public function index() { $user = Auth::user(); $allTvEpisodes = UserTvEpisode::with('TvEpisode')->where("user_id", "=", $user->id)->get(); return view('tvepisode.index',[ 'allTvEpisodes' => $allTvEpisodes ]); } }
UserTvEpisode model:
<?php namespace AppModels; use IlluminateDatabaseEloquentFactoriesHasFactory; use IlluminateDatabaseEloquentModel; use AppModelsTvEpisode; class UserTvEpisode extends Model { use HasFactory; protected $table = 'user_tv_episodes'; protected $primaryKey = 'id'; public $timestamps = true; protected $dataFormate ='h:m:s'; public function TvEpisode(){ return $this->belongsTo(TvEpisode::class)->withDefault();; } public function user(){ return $this->belongsTo(User::class); } }
TvEpisode model
<?php namespace AppModels; use IlluminateDatabaseEloquentFactoriesHasFactory; use IlluminateDatabaseEloquentModel; use AppModelsUserTvEpisode; class TvEpisode extends Model { use HasFactory; protected $table = 'tv_episodes'; protected $primaryKey = 'id'; public $timestamps = false; protected $dataFormate ='h:m:s'; public function userTvEpisodes(){ return $this->hasMany(UserTvEpisode::class); } }
What am I missing here?
Advertisement
Answer
If you are sure that there are no case sensitivity problems, you could try a couple of things:
- Check if the namespaces of your classes are correct. Do the classes exists in the right directory, models in
AppModels
and the controller inAppHttpControllers
? If not, move the class or change the namespace. - Are you sure the file exists on the server? Is the project on the server up-to-date with your local project?
- Run
composer dump-autoload
on the server to regenerate list of classes used in the project.
I am pretty sure one of the solution above will solve your problem.