'Class tableseeder does not exist (although it really exists) :S
I get this error when trying to migrate plus seeding my database using laravel 5.4
[ReflectionException] Class PostTagTableSeeder does not exist
In fact the file really exist with correct class name
seeds/PostTagTableSeeder.php
<?php
use Illuminate\Database\Seeder;
use App\Tag;
use App\Post;
class PostTagTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
//DB::table('post_tag')->delete();
$tag1 = Tag::find(1);
$tag2 = Tag::find(2);
$tag3 = Tag::find(3);
$tag4 = Tag::find(4);
$post1 = Post::find(1);
$post2 = Post::find(2);
$post3 = Post::find(3);
$post4 = Post::find(4);
$post5 = Post::find(5);
$post6 = Post::find(6);
$post7 = Post::find(7);
$post8 = Post::find(8);
$post9 = Post::find(9);
$post10 = Post::find(10);
$post11 = Post::find(11);
$post12 = Post::find(12);
$post13 = Post::find(13);
$post14 = Post::find(14);
$post15 = Post::find(15);
$post1->tags()->attach([$tag1->id,$tag2->id,$tag3->id]);
$post2->tags()->attach([$tag2->id,$tag4->id]);
$post3->tags()->attach([$tag2->id,$tag3->id,$tag4->id]);
$post4->tags()->attach([$tag4->id]);
$post5->tags()->attach([$tag2->id]);
$post6->tags()->attach([$tag2->id]);
$post7->tags()->attach([$tag2->id]);
$post8->tags()->attach([$tag1->id,$tag4->id]);
$post9->tags()->attach([$tag1->id,$tag4->id]);
$post10->tags()->attach([$tag3->id]);
$post11->tags()->attach([$tag1->id]);
$post12->tags()->attach([$tag1->id]);
$post13->tags()->attach([$tag3->id]);
$post14->tags()->attach([$tag1->id,$tag2->id,$tag4->id]);
$post15->tags()->attach([$tag1->id,$tag2->id,$tag4->id]);
}
}
seeds/DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call(UsersTableSeeder::class);
$this->call(AdminsTableSeeder::class);
$this->call(PostsTableSeeder::class);
$this->call(CategoriesTableSeeder::class);
$this->call(CategoryPostTableSeeder::class);
$this->call(TagsTableSeeder::class);
$this->call(PostTagTableSeeder::class);
$this->call(CommentsTableSeeder::class);
}
}
I have been struggling to fix this issue but yet i still get the same error
Solution 1:[1]
Had the same issue and running
composer dump-autoload
as suggested above by Vape and Oliver fixed the issue. Then re-run the migrations
php artisan migrate:refresh --seed
or the seeder
php artisan db:seed
and it'll run like a charm.
Solution 2:[2]
For some, due to your file structure or composer set-up, all you need to run is:
composer dump-autoload
or
composer.phar dump-autoload
Then, retry the seeder script.
Solution 3:[3]
When you have written your seeder, you may need to regenerate Composer's autoloader using the dump-autoload command: here
composer dump-autoload
Now you may use the db:seed Artisan command to seed your database.
Solution 4:[4]
If you have migrated to Laravel 8, you will need to namespace your seeder classes as follows:
<?php
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
}
}
Then, you have to import the seeder classes with the correct namespace.
Then, in your composer.json file, remove classmap block from the autoload section and add the new namespaced class directory mappings:
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
Then, run composer dump
Finally, the database/seeds directory should be renamed to database/seeders (if you have not yet renamed it)
This breaking change is documented here https://laravel.com/docs/8.x/upgrade#seeder-factory-namespaces
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tiago Martins Peres |
| Solution 2 | Oladipo |
| Solution 3 | tvs89 |
| Solution 4 | mutiemule |
