I Lukas
I Lukas

Reputation: 53

Class not found laravel 8 only on production

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.

:Error message

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?

Upvotes: 5

Views: 10608

Answers (2)

Igor
Igor

Reputation: 1131

The reason can be with OPCache (if you use it on production) and you just uploaded new files to a server and new PHP classes not indexed yet, so composer dump-autoload will not help you. You have to restart your php service to reindex all PHP instances in OPCache (this example for Ubuntu and Nginx with php 8.1 fpm):

sudo service php8.1-fpm restart

Also to make sure - restart your web server (for example nginx)

sudo service nginx restart

Upvotes: 1

Eric Landheer
Eric Landheer

Reputation: 2243

If you are sure that there are no case sensitivity problems, you could try a couple of things:

  1. Check if the namespaces of your classes are correct. Do the classes exists in the right directory, models in App\Models and the controller in App\Http\Controllers? If not, move the class or change the namespace.
  2. Are you sure the file exists on the server? Is the project on the server up-to-date with your local project?
  3. 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.

Upvotes: 11

Related Questions