Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Not Finding Eloquent Models

I'm brand new to Laravel and am having trouble loading my models in order to seed the database. I have tried both the Laravel 4 method of composer.json and the Laravel 5 PSR-4.

DatabaseSeeder.php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        \App\Models\Client::truncate();
        \App\Models\Module::truncate();
        \App\Models\ModuleConnection::truncate();

        Model::unguard();

        $this->call('ClientsTableSeeder');
        $this->call('ModulesTableSeeder');
        $this->call('ConncetionsTableSeeder');
    }

}

Client.php

These are in app\Models at the moment, but I have tried them in app next to Users.php

namespace App\Models;
use Illuminate\Database\Eloquent\Model as Eloquent;

class Client extends \Eloquent {
    proteced $fillable = ['id', 'name', 'email', 'created_at', 'updated_at'];
}

Module.php

namespace App\Models;
use Illuminate\Database\Eloquent\Model as Eloquent;

class Module extends \Eloquent {
    proteced $fillable = ['id', 'name', 'created_at', 'updated_at'];
}

ModuleConnection.php

namespace App\Models;
use Illuminate\Database\Eloquent\Model as Eloquent;

class ModuleConnection extends \Eloquent {
    proteced $fillable = ['id', 'clientid', 'moduleid', 'apiconn', 'created_at', 'updated_at'];
}

The error

This is when I run php artisan db:seed

[Symfony\Component\Debug\Exception\FatalErrorException]  
Class 'App\Models\Client' not found 

I'm sure it's something stupid I'm missing as a beginner! Thanks!

like image 730
Austin Heerwagen Avatar asked Apr 29 '15 15:04

Austin Heerwagen


1 Answers

First, you probably have to extend Eloquent and not \Eloquent. When you add a \ on your usage, you are telling PHP to find an Eloquent in its root namespace. So:

namespace App\Models;
use Illuminate\Database\Eloquent\Model as Eloquent;

class Module extends Eloquent {
    proteced $fillable = ['id', 'name', 'created_at', 'updated_at'];
}

Second, there's no need to do a composer dumpautoload because your App namespace was included by your Laravel instalation:

"autoload": {
    ...
    "psr-4": {
        "App\\": "app/"
    }
},

This is telling the autoloader to search for your namespaced classes in the app/ path.

The thing you have to check is if the file Client.php is in the folder:

app/Models/Client.php

And named as is. Other than that I don't see a problem in your code.

like image 95
Antonio Carlos Ribeiro Avatar answered Oct 04 '22 22:10

Antonio Carlos Ribeiro