首页 文章

Laravel 5 - 工匠种子[ReflectionException]类SongsTableSeeder不存在

提问于
浏览
173

当我运行 php artisan db:seed 时,我收到以下错误:

[ReflectionException] Class SongsTableSeeder does not exist

到底是怎么回事?

我的 DatabaseSeeder class:

<?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('SongsTableSeeder');
    }

}

我的 SongsTableSeeder class:

<?php

// Composer: "fzaninotto/faker": "v1.4.0"
use Faker\Factory as Faker;
use Illuminate\Database\Seeder;
use DB;

class SongsTableSeeder extends Seeder {

    public function run()
    {
        $faker = Faker::create();
        $songs = [];
        foreach(range(1, 10) as $index)
        {
            $songs[] = ['title' => $faker->words(rand(1,4))];
        }

        DB::table('songs')->insert($songs);

    }

}

12 回答

  • 15

    不要忘记 composer dump-autoloadcomposer.json 的autoload / classmap部分有关 . 如果您需要更改seeders目录或使用多个目录来存储播种机,请注意这一点 .

    "autoload": {
        "classmap": [
          "database/seeds",
          "database/factories"
        ],
    },
    
  • 0

    我做的是再次重新创建 Seeder ,它对我有用 .

    php artisan make:seeder UsersTableSeeder

  • 0

    我有同样的“反射异常”错误 . 解决方案是将类文件从dev复制到服务器 . 愚蠢的错误,但鉴于我们处理了多少文件,每次都很容易忘记将它们复制到服务器上 .

  • 488

    文件 SongsTableSeeder.php 应位于 database/seeds 目录或其子目录中 .

    你需要运行:

    composer dump-autoload
    

    然后:

    php artisan db:seed
    

    要么:

    php artisan db:seed --class=SongsTableSeeder
    
  • 3

    您需要将 SongsTableSeeder 放入 SongsTableSeeder.php 文件中与 DatabaseSeeder.php 文件所在的目录中 .

    你需要在你的控制台中运行:

    composer dump-autoload
    

    生成新的类映射,然后运行:

    php artisan db:seed
    

    我刚试过它 . 它在Laravel 5中没有问题

  • 0

    我在其中仅使用了两个类的SINGLE FILE:

    use Illuminate\Database\Seeder;
    use Illuminate\Database\Eloquent\Model;
    use App\Lesson;
    
    use Faker\Factory as Faker;
    
    class DatabaseSeeder extends Seeder {
    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //Lesson::truncate();
    
        Model::unguard();
    
        $this->call("LessonsTableSeeder");
    
    
    }
    
    }
    
    class LessonsTableSeeder extends Seeder {
    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
    
        $faker = Faker::create();
    
        foreach(range(1,30) as $index) {
    
            Lesson::create(['title' => $faker->sentence(5), 'body' => $faker->paragraph(4)]);
    
        }
    
    }
    
    }
    
  • 6

    SongsTableSeeder.php应该在database / seeds目录中

    控制台命令步骤:

    composer dump-autoload
    

    然后:

    php artisan cache:clear
    

    然后:

    php artisan optimize
    

    然后:

    php artisan db:seed
    

    要么:

    php artisan db:seed --class=SongsTableSeeder
    
  • 0

    我解决了这个问题:

    • 复制文件内容 .

    • 删除文件 .

    • 运行命令:php artisan make:seeder .

    • 将文件内容复制回此文件中 .

    发生这种情况是因为我对文件名进行了更改 . 我不知道为什么改变后它不起作用 .

  • 0

    如果您从任何其他项目复制了seeders文件,那么您需要运行artisan命令 php artisan db:seed 否则它没问题 .

  • 0

    如果我们的CustomTableSeeder与DatabaseSeeder在同一目录中,我们应该使用如下:

    $this->call('database\seeds\CustomTableSeeder');
    

    在我们的DatabaseSeeder文件中;然后会抛出另一个错误:“找不到DB类”然后我们应该将我们的数据库外观添加到我们的CustomTableSeeder文件中,如下所示:

    use Illuminate\Support\Facades\DB;
    

    它对我有用!

  • 1

    Laravel需要一个“root”播种器类:

    请参阅artisan帮助页面:

    $ php artisan help db:seed   
    
    Usage:
      db:seed [options]
    
    Options:
          --class[=CLASS]        The class name of the root seeder [default: "DatabaseSeeder"]
          --database[=DATABASE]  The database connection to seed
          --force                Force the operation to run when in production.
      -h, --help                 Display this help message
      -q, --quiet                Do not output any message
      -V, --version              Display this application version
          --ansi                 Force ANSI output
          --no-ansi              Disable ANSI output
      -n, --no-interaction       Do not ask any interactive question
          --env[=ENV]            The environment the command should run under
      -v|vv|vvv, --verbose       Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
    
    Help:
      Seed the database with records
    

    如果要删除DatabaseSeeder,则必须使用 --class 选项定义seeder类 .

  • 0

    我正在运行最新的Laravel 5 dev版本,如果你更改了命名空间,你需要像这样调用你的种子类:

    $this->call('\todoparrot\TodolistTableSeeder');
    

    显然,您需要将 todoparrot 替换为您指定的命名空间 . 否则我收到原始问题中指出的相同错误 .

相关问题