20

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 errorenter image description here

4

4 Answers 4

49

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.

3
  • 1
    composer dump-autoload worked for me thank you so much !
    – FabienChn
    Commented Jul 24, 2017 at 10:47
  • why do I have to always use it when I make a new seeder?? Commented Jan 15, 2020 at 8:06
  • The seeder files are in the database directory and are not namespaced. There is more info in the discussion here stackoverflow.com/questions/27426139/…
    – kerrin
    Commented Jan 15, 2020 at 23:11
17

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.

1

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.

1

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

1
  • 1
    My migrations were from a previous version and ported into Laravel 8. I updated the namespace and added use DB; then ran composer dump-autoload to update my autoload.
    – stoi2m1
    Commented Feb 25, 2023 at 3:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.