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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36206742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:44:45  来源:igfitidea点击:

Class 'UserTableSeeder' does not exist - Laravel 5.0 [php artisan db:seed]

phplaravel

提问by thirdriver

I'm trying a basic php artisan db:seed after migrating my database but it keeps returning the title error in cmd -[ReflectionException] Class 'UserTableSeeder' does not exist

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

Things I Have Tried

我尝试过的事情

  • Change the namespace of the 'UserTableSeeder.php' File 'namespace Database\seeds;' and 'use Database\seeds\UserTableSeeder;' in the 'DatabaseSeeder.php' File
  • 更改“UserTableSeeder.php”文件“namespace Database\seeds;”的命名空间 和“使用 Database\seeds\UserTableSeeder;” 在“DatabaseSeeder.php”文件中

Below is the migrations

下面是迁移

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

Below is the UserTableSeeder.php

下面是 UserTableSeeder.php

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

class UserTableSeeder extends Seeder {

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

        User::create(['email' => '[email protected]']);
    }
}

Below is the DatabaseSeeder.php

下面是 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');
    }
}

回答by Nick

Run composer dumpautoloadafter creating files in the database/ folder.

composer dumpautoload在 database/ 文件夹中创建文件后运行。

Why?

为什么?

Check the composer.jsonautoload section and you'll see the database/folder is loaded by "classmap" (source):

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

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

The Composer docsdescribe classmap as:

Composer文档将 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.

在安装/更新期间,类映射引用全部组合成一个键 => 值数组,该数组可以在生成的文件 vendor/composer/autoload_classmap.php 中找到。该映射是通过扫描给定目录/文件中所有 .php 和 .inc 文件中的类来构建的

您可以使用类映射生成支持为所有不遵循 PSR-0/4 的库定义自动加载。要配置它,您可以指定所有目录或文件来搜索类。

Emphasis added. You need to run the composer dumpautoloadcommand to generate a new classmap every time you add a file to database/, otherwise it will not be autoloaded.

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

The app/folder, by contrast, uses the PSR-4standard for converting a fully qualified class name to a filesystem path. This is why you don't need to dumpautoloadafter adding files there.

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

回答by Wouter Van Damme

Try changing

尝试改变

  $this->call('UserTableSeeder');

to

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

and try running

并尝试运行

 composer dump-autoload

回答by Inamur Rahman

Sometimes the code is correct butyou need to run the following command in order to run the seeder command. First Run this command

有时代码是正确的,但您需要运行以下命令才能运行 Seeder 命令。首先运行这个命令

composer dumpautoload

Then seed the seeder

然后播种播种机

php artisan db:seed --class=CreateUsersTable

I hope it will work

我希望它会起作用

回答by Sumit Kumar Gupta

When we change or delete the Controller file or another file then their file should be removed from everywhere in code. You need to run command to refresh your composer

当我们更改或删除控制器文件或其他文件时,应从代码中的任何地方删除它们的文件。您需要运行命令来刷新您的作曲家

composer dump-autoload