Home » Php » php – ReflectionException – Class DatabaseSeeder does not exist , Laravel Seeder

php – ReflectionException – Class DatabaseSeeder does not exist , Laravel Seeder

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&Answers:

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 dumpautoload and try php artisan db:seed

Answer:

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
        ));
    }
}

Answer:

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

Then it will works fine.

Answer:

Solved: by adding

namespace database\seeds;

and then running command:

composer dump-autoload –no-dev

Answer:

Does adding the –no-dev flag help?

composer dump-autoload --no-dev

source