'UserTableSeeder' 类不存在 - Laravel 5.0 \[php artisan db:seed\]

新手上路,请多包涵

我正在尝试一个基本的 php artisan db:seed 迁移我的数据库后,但它一直在 cmd 中返回标题错误 -[ReflectionException] Class ‘UserTableSeeder’ 不存在

我尝试过的事情

  • 更改 ‘UserTableSeeder.php’ 文件 ‘namespace Database\seeds;’ 的命名空间和’使用数据库\种子\用户表种子;’在“DatabaseSeeder.php”文件中

下面是迁移

<?php

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

    class CreateUsersTable extends Migration {

        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('users', function(Blueprint $table)
            {
                $table->increments('id');
                $table->string('name');
                $table->string('email')->unique();
                $table->string('password', 60);
                $table->rememberToken();
                $table->timestamps();
            });
        }

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

下面是 UserTableSeeder.php

 <?php
use App\User;
use Illuminate\Database\Seeder;

class UserTableSeeder extends Seeder {

    public function run()
    {
        DB::table('users')->delete();

        User::create(['email' => 'foo@bar.com']);
    }
}

下面是 DatabaseSeeder.php

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

原文由 thirdriver 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 570
1 个回答

在你的类中添加命名空间。

 <?php

namespace Database\Seeders;
...

之后运行 composer dump-autoload

原文由 user2511140 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题