datenshidon
datenshidon

Reputation: 121

Lumen 5.6 - php artisan db:seed got error 'Class DatabaseSeeder does not exist'

I'm trying to seed with Lumen 5.6.3 and executed the command: php artisan db:seed.

Then I got error, saying

In Container.php line 767:
Class DatabaseSeeder does not exist

In my database/seeds directory, DatabaseSeeder.php does exist. I've just copied the source in Lumen's official document and the source is like below.

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
    }
}

I've googled many times to solve this error and of course tried composer dump-autoload, composer dump-autoload -o, composer dump-autoload --no-dev several times and the situation has never changed.

I also checked my composer/autoload_classmap.php and there is 'DatabaseSeeder' => $baseDir . '/database/seeds/DatabaseSeeder.php' so I looks like autoload does work correctly.

I really appreciate any advices or comments. Thank you.

Upvotes: 1

Views: 3413

Answers (3)

datenshidon
datenshidon

Reputation: 121

I set a wrong value for bootstrap/app.php.

I set like the below.

require_once __DIR__.'/../../vendor/autoload.php';

After I modified this part like following, I could run db:seed command correctly.

require_once __DIR__.'/../vendor/autoload.php';

Upvotes: 0

Jio M.
Jio M.

Reputation: 114

To fix this issue, you have to tweak your composer.json in order for php artisan db:seed to work

By default, Lumen has placed the database directory under autoload-dev.
"autoload-dev": { "classmap": [ "tests/", "database/" ] },

To solve this, simple put the classmap together with your database directory under autoload

"autoload": { "psr-4": { "App\\": "app/" }, "classmap": [ "database/" ] },

after tweaking run composer update command in order for the tweak to work.

Upvotes: 2

Mohamed Baklouti
Mohamed Baklouti

Reputation: 121

You can use php artisan db:seed with lumen. The command is: php artisan make:seeder Seedername. For example you can use php artisan make:seeder UsersTableSeeder to create table seeder for the user. The file will be created in the folder database\seeds.

use Illuminate\Database\Seeder;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\User::class, 10)->create();
    }
}

This will create 10 example for the user class. Then you should cinfigure the databaseseeder file

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();
        // Register the user seeder
        $this->call(UsersTableSeeder::class);
        Model::reguard();
    }
}

Upvotes: 1

Related Questions