14

I have database seeder clases in diferent folder. When i write db:seed the console shows this error:

[ReflectionException]   Class DatabaseSeeder does not exist , Laravel Seeder

One class is this:

namespace Database\Seeds;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

use TiposCompromisosTableSeeder;

class DatabaseSeeder extends Seeder {

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

            Eloquent::unguard();

            $this->call('TiposCompromisosTableSeeder');
    }

}

and my other class is

namespace Database\Seeds;

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class TiposCompromisosTableSeeder extends Seeder{

    public function run(){

        DB::table('tipos')->insert(array(
            'nombre' => 'prioridad',
            'tabla' => 'compromisos',
            'str1' => 'baja',
            'int1' => 1
        ));
    }
}

I've tried to use

composer dump-autoupload 

but doesn't work.

As you can see, I have both clases in the same namespace.

Help please.

Iván Casanova
  • 351
  • 1
  • 6
  • 16

8 Answers8

18

If you've recently upgraded your Laravel version, check your composer.json

Your "autoload" section should look something like the snippet below

NOTE: you may have to add the "database" entry under the "classmap"

"autoload": {
        "classmap": [
          "app/Library",
          "app/Models",
          "database"
        ],
        "psr-4": {
            "App\\": "app/"
        },
        "files": [
            "app/Library/helpers.php"
        ]
    },

You should then run composer dump-autoload and try php artisan db:seed

Jagjot
  • 5,816
  • 2
  • 24
  • 41
vladko
  • 1,013
  • 14
  • 21
  • Great ... after add this line into composer and run composer i everything OK and errors gone forever ;) Thank you – saber tabatabaee yazdi Mar 02 '20 at 12:58
  • 1
    Adding this change and running `composter dump-autoload` didn't work for some reason, but worked after I ran `composer install`. – xyz Mar 21 '22 at 09:37
6

remove your namespace definition in two classes, and use "composer dump-autoload".

Then it will works fine.

alyosha
  • 315
  • 5
  • 11
6

You should add this line into composer.json file inside "psr-4":

"Database\\Seeders\\": "database/seeders/"

it means that should be something like:

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Modules\\": "Modules/",
        "Database\\Seeders\\": "database/seeders/"
    }
},
DolDurma
  • 15,753
  • 51
  • 198
  • 377
3

There is a need it only one change, rename the folder seed to seeders.

ahmedkandil
  • 477
  • 3
  • 6
2

Just put it all in the DatabaseSeeder.php file like this:

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder {

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

            Eloquent::unguard();

            $this->call('TiposCompromisosTableSeeder');
    }

}

class TiposCompromisosTableSeeder extends Seeder{

    public function run(){

        DB::table('tipos')->insert(array(
            'nombre' => 'prioridad',
            'tabla' => 'compromisos',
            'str1' => 'baja',
            'int1' => 1
        ));
    }
}
Brian H.
  • 505
  • 2
  • 12
  • Thanks but , what I should do if I would want put the seeder classes in two files ? – Iván Casanova May 23 '15 at 04:39
  • The Laravel code I have in the latest install doesn't have the DatabaseSeeder class in a namespace. If you're going to name space it, as well as the other class, you may need to register them in the IoC container before artisan can find them. Artisan uses the DatabaseSeeder class by default, which it finds without the namespace. It would just be much easier leaving it as is and put your other classes in the same file below it, so they can be found without having to register them (which I'm not even sure will work from the console). – Brian H. May 23 '15 at 04:58
  • you are only removing the namespaces. This is not a solution – Ricardo Vigatti Mar 02 '16 at 04:15
  • @ricardovigatti it is the solution. Laravel seeds have no namespace, therefore the CLI command `db:seed` will have no reference to a namespace, just a class in the root. However, multiple classes / file is not good. – David Barker Aug 01 '17 at 16:21
0

Solved: by adding

namespace database\seeds;

and then running command:

composer dump-autoload --no-dev

0

Also make sure database folder exist inside your project. If you accidently delete the database folder and run migration command. You will get this error too.

Hasitha Diluka
  • 766
  • 6
  • 13
-3

Does adding the --no-dev flag help?

composer dump-autoload --no-dev

source

Community
  • 1
  • 1