简体   繁体   中英

Laravel 5.0 db:seed class not found fatal error

I've tried everything I can think of but can't seem to resolve this issue, I'm sure it's something minor that I should be able to work out but it has me stumped.

I have already run composer dump-autoload with little success.

Whenever I use the artisan db:seed command I get the following error:

PHP Fatal error: Class 'App\\Models\\Company' not found in X:\\Development\\laravelApplication\\database\\seeds\\DatabaseSeeder.php on line 60

app/Http/Models/Company.php

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Company extends Model {

    //
    public function configurations() {
        return $this->hasMany('App\Models\Configuration');
    }
    public function users() {
        return $this->hasMany('App\Models\Users');
    }
    public function admins() {
        return $this->users()->where('admin', 1)->get();
    }

}

database/seeds/DatabaseSeeder.php

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\Models\Company;

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();

        $this->call('CompaniesTableSeeder');
        $this->command->info('Companies table seeded!');

        $this->call('UserTableSeeder');
        $this->command->info('User table seeded!');
    }

}

/**
 * Seed the Users table
 * @return void
 */
class UserTableSeeder extends Seeder
{

    function run()
    {
        $needlers_id = Company::where('name', 'Needlers LTD')->first()->id;
        DB::table('users')->delete();
        User::create([
            'company_id' => $needlers_id,
            'name' => 'Joe Ware',
            'email' => 'joe.ware@needlers.co.uk',
            'password' => 'needlers123',
            'phone' => '07123456789',
            'admin' => 1,
            'screen' => 0,
        ]);
    }
}

/**
 * Seed the Users table
 * @return void
 */
class CompaniesTableSeeder extends Seeder
{

    function run()
    {
        DB::table('companies')->delete();
        Company::create([
            'name' => 'Company name',
            'tier' => 'platinum'
        ]);
    }
}

Can anybody point out what I am missing / doing incorrectly?

Try to use this:

Company::create([
        'name' => 'Needlers LTD',
        'tier' => 'platinum'
    ]);

Or:

\App\Models\Company::create([
            'name' => 'Needlers LTD',
            'tier' => 'platinum'
        ]);

instead of this:

App\Models\Company::create([
        'name' => 'Needlers LTD',
        'tier' => 'platinum'
    ]);

Also, make sure you have Models namespace autoloaded, in composer.json use something like this:

"autoload": {
    "classmap": [
        "database",
        "app/Models"
    ],

Try it

composer dump

after run.

php artisan db:seed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM