【问题标题】:Laravel Seeder not found未找到 Laravel 播种机
【发布时间】:2019-11-02 17:35:44
【问题描述】:

我在创建表后的迁移过程中运行了一些播种机。这是我的迁移文件create_institutions_table

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateInstitutionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('institutions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('code');
            $table->timestamps();
            $table->softDeletes();
        });

        $seeder = new InstitutionsSeeder();
        $seeder->run();

        $seeder2 = new UsersSeeder();
        $seeder2->run();

        Schema::table('users', function (Blueprint $table) {
            $table->foreign('institution_id')->references('id')->on('institutions');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('institutions');
    }
}

这是InstitutionsSeeder

use Illuminate\Database\Seeder;

class InstitutionsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('institutions')->insert([
            'name' => 'Institution One',
            'code' => 'I1',
        ]);

    }
}

这是UsersSeeder

use Illuminate\Database\Seeder;

class UsersSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('users')->insert([
            'first_name' => 'Admin',
            'last_name' => 'Istrator',
            'email' => 'admin@example.com',
            'institution_id' => '1',
            'password' => '$2y$10$/wYQaaaaaaagrtyh64gbdt4yuhr32l4VmFHI.sINMR/9LXsj1MTy',
        ]);
    }
}

据我所知,播种机之间没有真正的区别,但是在尝试实例化 UsersSeeder 类时迁移失败,而 InstitutionsSeeder 工作正常。这是我从php artisan migrate:fresh 命令得到的异常:

   Symfony\Component\Debug\Exception\FatalThrowableError  : Class 'UsersSeeder' not found

  at H:\code\MyProject\database\migrations\2019_06_17_224612_create_institutions_table.php:27
    23| 
    24|                 $seeder = new InstitutionsSeeder();
    25|                 $seeder->run();
    26| 
  > 27|                 $seeder2 = new UsersSeeder();
    28|                 $seeder2->run();
    29| 
    30|                 Schema::table('users', function (Blueprint $table) {
    31|                         $table->foreign('institution_id')->references('id')->on('institutions');

  Exception trace:

  1   CreateInstitutionsTable::up()
      H:\code\MyProject\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:379

  2   Illuminate\Database\Migrations\Migrator::Illuminate\Database\Migrations\{closure}()
      H:\code\MyProject\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:388

  Please use the argument -v to see more details.

为什么UsersSeeder 不起作用?

【问题讨论】:

  • 在迁移中运行数据库种子是否有特定原因?如果没有,我强烈建议您改为在数据库播种器中运行这些。这也可能会解决您的问题...

标签: php laravel laravel-5.8 laravel-migrations


【解决方案1】:

两种可能的解决方案:

  1. 检查类的命名空间

  2. 运行 composer dump-autoload: composer dump-autoload(你可以在这里read the docs

【讨论】:

  • composer dump-autoload 成功了。我正在回到 PHP 生态系统,所以这是非常宝贵的信息,谢谢!我现在意识到它变得不同步,因为我复制了文件而不是使用相应的 php artisan make 命令。我了解,尽管您已经概述了 Laravel 文档中指示的播种数据的“按书本”方式,但由于超出此问题/答案范围的原因,我无法做到这一点,因此我可能会对其进行编辑分手。再次感谢!
  • 很高兴为您提供帮助!
【解决方案2】:

每次创建新的播种机时运行composer dump-autoload 命令。 之后,只需使用php artisan db:seed 命令运行播种机。 希望这会奏效!

【讨论】:

    猜你喜欢
    • 2018-12-27
    • 2013-05-25
    • 2020-10-03
    • 2014-12-08
    • 2014-09-05
    • 1970-01-01
    • 2022-11-27
    • 2023-01-12
    相关资源
    最近更新 更多