【问题标题】:Class tableseeder does not exist (although it really exists) :S类 tableseeder 不存在(尽管它确实存在):S
【发布时间】:2017-10-17 03:18:29
【问题描述】:

我在尝试使用 laravel 5.4 迁移和播种我的数据库时遇到此错误

[ReflectionException] 类 PostTagTableSeeder 不存在

事实上该文件确实存在且具有正确的类名

种子/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]);
    }
}

种子/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);
    }
}

我一直在努力解决这个问题,但我仍然遇到同样的错误

【问题讨论】:

标签: php laravel laravel-5.4


【解决方案1】:

遇到同样的问题并正在运行

composer dump-autoload

如上所述,Vape 和 Oliver 已解决了该问题。然后重新运行迁移

php artisan migrate:refresh --seed

或播种机

php artisan db:seed

它会像魅力一样运行。

【讨论】:

  • composer dump-autoload 为我工作,非常感谢!
  • 为什么我在制作新播种机时必须始终使用它??
  • 种子文件在数据库目录中,没有命名空间。此处的讨论中有更多信息stackoverflow.com/questions/27426139/…
【解决方案2】:

对于某些人来说,由于您的文件结构或作曲家设置,您只需要运行:

composer dump-autoload

composer.phar dump-autoload

然后,重试播种脚本。

【讨论】:

    【解决方案3】:

    编写完播种器后,您可能需要使用 dump-autoload 命令重新生成 Composer 的自动加载器:here

    composer dump-autoload
    

    现在您可以使用 db:seed Artisan 命令为您的数据库播种。

    【讨论】:

      【解决方案4】:

      如果你已经迁移到 Laravel 8,你需要将你的种子类命名空间如下:

      <?php
      
      namespace Database\Seeders;
      
      use App\Models\User;
      use Illuminate\Database\Seeder;
      
      class DatabaseSeeder extends Seeder
      {
          
          public function run()
          {
              
          }
      }
      

      然后,您必须使用正确的命名空间导入播种器类。

      然后,在您的 composer.json 文件中,从自动加载部分删除 classmap 块并添加新的命名空间类目录映射:

      "autoload": {
          "psr-4": {
              "App\\": "app/",
              "Database\\Factories\\": "database/factories/",
              "Database\\Seeders\\": "database/seeders/"
          }
      },
      

      然后,运行composer dump

      最后,database/seeds 目录应该重命名为 database/seeders(如果你还没有重命名的话)

      此重大更改记录在此处https://laravel.com/docs/8.x/upgrade#seeder-factory-namespaces

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-20
        • 1970-01-01
        • 1970-01-01
        • 2022-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多