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 "App\Models\TvEpisode" 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 App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\UserTvEpisode;
use App\Models\TvEpisode;
use Auth;
class TvEpisodeController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
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 App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\TvEpisode;
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 App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\UserTvEpisode;
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?
If you are sure that there are no case sensitivity problems, you could try a couple of things:
App\Models and the controller in App\Http\Controllers? If not, move the class or change the namespace.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.