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

标签 php laravel

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

我尝试过的事情

  • 更改'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');
    }
}

最佳答案

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

为什么?

检查 composer.json 自动加载部分,您会看到 database/ 文件夹由“classmap”(source)加载:

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

Composer docs将 classmap 描述为:

The classmap references are all combined, during install/update, into a single key => value array which may be found in the generated file vendor/composer/autoload_classmap.php. This map is built by scanning for classes in all .php and .inc files in the given directories/files.

You can use the classmap generation support to define autoloading for all libraries that do not follow PSR-0/4. To configure this you specify all directories or files to search for classes.

添加了重点。每次向database/添加文件时都需要运行composer dumpautoload命令生成新的classmap,否则不会自动加载。

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

关于php - 类 'UserTableSeeder' 不存在 - Laravel 5.0 [php artisan db :seed],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36206742/

相关文章:

php - 206.2 kB(加载 204.8 kB)在 LONGBLOB 值中前置

javascript - Window.location 在 ajax 中被取消

php - 使用youtube API获取有关youtube channel 的受众特征信息

php - 错误 $HTTP_RAW_POST_DATA 已弃用,请使用 php ://input stream instead. "

php - Laravel 中具有多对多关系的工厂种子数据透视表

php - Laravel Dusk loginAs 不工作,当使用 DatabaseMigration 特性时

javascript - 如何使用函数绘制高位图?

javascript - 如何在javascript中打印php数组

javascript - 有没有办法用 php 检查 url 中的特定参数?

php - Laravel - 根据用户状态显示和隐藏元素