Forum

Laravel5 Seeder, ReflectionException, "class ?????TableSeeder does not exist" in...

Not sure if you can post questions about Laravel 5 yet?

Running "php artisan db:seed" in Laravel 5 raises a ReflectionException with message "Class ?????TableSeeder does not exist". It does exist but I can't seem to figure out why Laravel can't find it.

Simplified for this post but the seeder looks like this .......

<?php

use Illuminate\Database\Seeder; use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder {

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

    // $this->call('UserTableSeeder');
    $this->call('ContactsTableSeeder');
}

}

and the seeder Class looks like this........

<?php namespace database\seeds;

use Illuminate\Database\Seeder; use App\Models\Contact;

class ContactsTableSeeder extends Seeder {

public function run()
{
    DB::table('contacts')->delete();

    Contact::create([
        'name' => 'The Name',
        'email' => [email protected]',
        'charset' => 'utf-8'
    ]);

}

}

I'd like to say I've tried everything but I clearly haven't coz it's still not working.

Is it namespacing? Is it because I have my models in a subfolder?

composer selfupdate; composer update; composer dumpautoload; php artisan migrate; ...are all successful

php artisan db:seed; php artisan db:seed --class="database\seeds\ContactsTableSeeder"; .....all fail

I know I've missed something simple and stupid but for the life of me I can't figure it out.

Your help will be very gratefully received.

ghkdev
ghkdev
  • 1 year ago

Solved my own problem...... but found another!

Add this to the top of DatabaseSeeder.php "use database\seeds\ContactsTableSeeder;"

Then the call in ContactsTableSeeder.php becomes "$this->call(ContactsTableSeeder::class);"

This way we let PHP do all the work of classes and namespacing.

However "php artisan db:seed" now produces this error..... "Class 'database\seeds\DB' not found in....."

ghkdev
Solution

It looks like your TableSeeder needs "use Illuminate\Support\Facades\DB;"

Seeder now works just fine.

There's another simple solution here: Put your "ContactsTableSeeder" class into DatabaseSeeder.php like below code:

<?php
use Illuminate\Database\Seeder; 
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder {

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

       // $this->call('UserTableSeeder');
       $this->call('ContactsTableSeeder');
   }

}


class ContactsTableSeeder extends Seeder {

   public function run()
   {
       DB::table('contacts')->delete();

       Contact::create([
           'name' => 'The Name',
           'email' => [email protected]',
           'charset' => 'utf-8'
       ]);

   }

}

And execute "php artisan db:seed" again! It will work!

hi all,

I have the some problem as above

here's my code

//DatabaseSeeder.php

//use database\seeds\InsertUser;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder {

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

        // $this->call('UserTableSeeder');
        $this->call('InsertUser');
    }

}
//InsertUser.php

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

class InsertUser extends Seeder
{
    public function run()
    {
        User::create(
                array(
                        'nama'      => 'This',
                        'email'     =>  [email protected]',
                        'password'  =>  Hash::make('ikhsan123'),
                    )
            );
    }
}

but it still does not work.. It appears class 'User' not found.

Somebody help me.. please

@thisem

Check namespaces, by default DatabaseSeeder are not namespaced, but I bet your User class is under Laravel 5.

Try \App\User::create(

You should never forget, even when it sounds dummy to dump the autoload files with the command "composer dump-autoload" to make the seeders work properly.

Greets from México

VictorAvelar

The composer dump-autoload worked perfectly. Thank you!

kajackdfw You can also use the Model at the top if you call it often in the class:

<?php

use Illuminate\Database\Seeder;
use App\User;

class UserTableSeeder extends Seeder {
    public function run()
    {
        User::create(array(

I've the same problem right here http://laravel.io/forum/06-13-2015-how-to-insert-values-with-migration

VictorAvelar said:

You should never forget, even when it sounds dummy to dump the autoload files with the command "composer dump-autoload" to make the seeders work properly.

Greets from México

Thanks a lot @VictorAvelar that fixed the problem. It's always simple details like that haha. Cheers mate!

Thank you @VictorAvelar!

That's why by default Lumen Composer file maps the database path with the classmap strategy. That means that every time Composer dumps the autoloader, it creates a static map of all the classes available inside that folder. So every time we add a new class there we need to manually re-dump the autoloader script:

composer dump-autoload

Thanks @VictorAvelar.

VictorAvelar said:

You should never forget, even when it sounds dummy to dump the autoload files with the command "composer dump-autoload" to make the seeders work properly.

Greets from México

This is the ultimate solution ! Thanks a lot.

Hello guys,

I had the same issue, and every thing you guys suggested I checked all of them, all things are OK. but still I was having the same problem.

And I figure out on my own, that some thing wrong with caching, I assumed some code might be cached and that's why it's not able to find newly created seed class.

So, I just run the command

"composer update"

And it's works for me. Thanks alot, for helping me, and I hope this option may help some one else.

In my case I haven't had in my composer.json

"classmap": [ ... "database/migrations" ]

VictorAvelar said:

You should never forget, even when it sounds dummy to dump the autoload files with the command "composer dump-autoload" to make the seeders work properly. Greets from México

It works! Thanks @VictorAvelar

composer dump-autoload

ghkdev said:

It looks like your TableSeeder needs "use Illuminate\Support\Facades\DB;"

Seeder now works just fine.

This did the job :+1: