首页 文章

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

提问于
浏览
13

我正在尝试一个基本的php工匠db:seed迁移我的数据库但它一直在cmd中返回 Headers 错误 - [ReflectionException]类'UserTableSeeder'不存在

我试过的事情

  • 更改'DatabaseSeeder.php'文件中'UserTableSeeder.php'文件'namespace Database\seeds;'和'use Database\seeds\UserTableSeeder;'的命名空间

以下是迁移

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

2 回答

  • 9

    在数据库/文件夹中创建文件后运行 composer dumpautoload .

    Why?

    检查 composer.json autoload部分,您将看到 database/source)加载了 database/ 文件夹:

    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    

    Composer docs将类映射描述为:

    在安装/更新期间,类映射引用全部组合成单个key => value数组,该数组可以在生成的文件vendor / composer / autoload_classmap.php中找到 . 通过扫描给定目录/文件中的所有.php和.inc文件中的类来构建此映射 . 您可以使用类映射生成支持为所有不遵循PSR-0/4的库定义自动加载 . 要配置它,请指定要搜索类的所有目录或文件 .

    强调补充说 . 每次将文件添加到 database/ 时,都需要运行 composer dumpautoload 命令来生成新的类图,否则它将不会自动加载 .

    相比之下, app/ 文件夹使用PSR-4标准将完全限定的类名转换为文件系统路径 . 这就是为什么在那里添加文件后不需要 dumpautoload 的原因 .

  • 39

    尝试改变

    $this->call('UserTableSeeder');
    

    $this->call(UserTableSeeder::class);
    

    并尝试运行

    composer dump-autoload
    

相关问题