Back

Laravel5 Seeder, ReflectionException, "class ?????TableSeeder does not exist" in Illuminate/Container/Container.php:776


ghkdev posted 2 years ago

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' => 'thename@email.com', '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 replied 2 years 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 replied 2 years ago Solution

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

Seeder now works just fine.

brad0924 replied 2 years ago

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' => 'thename@email.com',
           'charset' => 'utf-8'
       ]);

   }

}

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

thisem replied 2 years ago

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'		=>	'this@gmail.com',
						'password'	=>	Hash::make('ikhsan123'),
					)
			);
	}
}

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

Somebody help me.. please

tkprocat replied 2 years ago

@thisem

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

kajackdfw replied 2 years ago

Try \App\User::create(

VictorAvelar replied 2 years ago

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

arikin replied 2 years ago

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(
aanmtn replied 2 years ago

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

yahyazini replied 1 year ago

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!

mboldrick replied 1 year ago

Thank you @VictorAvelar!

marcoriedel replied 1 year ago

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

desertjinn replied 1 year ago

Thanks @VictorAvelar.

gdroid7 replied 1 year ago

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.

AdiechaHK replied 1 year ago

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.

Hlorofos replied 1 year ago

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

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

ethanliew replied 10 months ago

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

saulchelewani replied 7 months ago
composer dump-autoload
omitobi replied 6 months ago

ghkdev said:

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

Seeder now works just fine.

This did the job :+1:

aabi01 replied 5 months ago

composer dump-autoload worked for me !!!

oburatongoi replied 5 months ago

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

Perfect!


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us