简体   繁体   中英

Laravel 5 class DatabaseSeeder not found when running php Artisan db:seed

I'm new to Laravel, When I run php artisan db:seed i recieve the following message:

[ReflectionException] Class DatabaseSeeder does not exist

I already have run composer dump-autoload sadly without any result. My class is located in the default folder /seeds

The code from the class:

<?php namespace database\seeds;

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\User;
use App\Country; 

class DatabaseSeeder extends Seeder {

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

        $this->call('CountryTableSeeder');
        $this->call('UserTableSeeder');
    }


}

class UserTableSeeder extends Seeder {

    public function run() {
        DB::table('users');

        User::Create(['username' => 'Bart', 'email'=>'bart@example.com', 'password' => Hash::make('password')]); 
    }
}

class CountryTableSeeder extends Seeder { 
    public function run() {
        DB::table('country');

        Country::Create(['country_name' => 'Nederlander']);
    }
}

What I'm doing wrong?

First you should remove namespace database\\seeds; as @JoeCoder suggested in comment.

And the second thing is that you should not put many classes inside one file (as you probably did looking at your question). Each class should be placed in separate file.

if you've tried everything else and still getting Class DatabaseSeeder does not exist , take a look at your composer.json

There should be a section that looks something like:

    "autoload": {
        "classmap": [
            "app/Library",
            "app/Models"
        ],
...

You should add two more lines here to tell autoloader where else it should look for your classes.

So final result will be:

    "autoload": {
        "classmap": [
            "app/Library",
            "app/Models",
            "database/seeds",
            "database/migrations"
        ],
...

This will add both seeds and migrations directory to your autoloader class path.

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