Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReflectionException - Class DatabaseSeeder does not exist , Laravel Seeder [duplicate]

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.

like image 315
Iván Casanova Avatar asked May 23 '15 02:05

Iván Casanova


4 Answers

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/"
    }
},
like image 61
DolDurma Avatar answered Oct 10 '22 08:10

DolDurma


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

like image 23
vladko Avatar answered Oct 10 '22 10:10

vladko


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

Then it will works fine.

like image 20
alyosha Avatar answered Oct 10 '22 10:10

alyosha


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
        ));
    }
}
like image 38
Brian H. Avatar answered Oct 10 '22 08:10

Brian H.