Home > database >  Laravel: Class "App\Models\Device" not found in web.php (only in production)
Laravel: Class "App\Models\Device" not found in web.php (only in production)

Time:10-12

i'm trying to make an example project to learn laravel. I set up the project on my computer and everything worked fine: I have my web.php that routes / to a view, but before that it collects devices so it can show you some data.

I decided to try and host it on a Debian AWS instance so I cloned my repo in /var/www/, migrated the database, wrote the .env, composer install, set folder ownership to admin user and permissions to storage and bootstrap.

Once i configured nginx i tried it by navigating to the url and the answer was Class "App\Models\Device" not found.

I checked the namespaces, file names, I even read that debian is a sucker for caps sensitivity so i double checked every capital letter in every name and since app folder is named app and not App i even tried to import it as app\Models\Device but to no avail.

I also tried with composer dump-autoload as many on SO suggested, but nothing changed at all.

Am i missing something?

Device.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Device extends Model
{
    use HasFactory;
    use SoftDeletes;


    protected $fillable = [
        'serial',
        'livello_acqua',
        'umidita',
        'temperatura',
        'livello_mangime',
        'stato_rele_1',
        'stato_rele_2',
        'stato_rele_3',
        'stato_rele_4',
        'descrizione',
    ];

    public static function findBySerial($serial){
        return Device::where('serial', $serial)->get();
    }

    public static function findByUser($id){
        return Device::where('user_id', $id)->get();
    }

}

web.php

<?php

use app\Models\Device; //\app\Models\Device - App\Models\Device - \App\Model\Device
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    $devices = Device::all();

    return view('index')->with(['devices' => $devices]);
});

Structure:

- app
- - Models
- - - Device
- - ...
- ...
- routes
- - web.php
- ...

CodePudding user response:

I think i'm going nuts, i just tried again with App\Models\Device and got the error, re-changed it to app\Models\Device and got the error again and finally re-re-changed it to App\Models\Device and now it works...... have no idea why or how...

CodePudding user response:

Try to import it like so use App\Models\Device; instead of use app\Models\Device;

See this link also

  • Related