ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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

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

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

Below is the 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');
    }
}
did you try to import this (UserTableSeeder) class?

N
Nick

Run composer dumpautoload after creating files in the database/ folder.

Why?

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

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

The Composer docs describe classmap as:

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.

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

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


I added two other seed files and they ran fine. The third one got 'stuck'. I spent a half hour trying to figure out what I did wrong. Your solution worked like a charm. Not in love with the laravel developers right now, but I am in love with you! ;-)
Thanks. Added a folder under app/ for more organized structure and needed to update composer.json to map. This worked for me :)
This is helpful, but the best solution is not to run composer dumpautoload. If you add "Database\\Seeders\\": "database/seeds/" in the psr-4 section, and remove its entry from the classmap section, then everything just works as expected, with no need to run extra commands when you add a new class.
I
Inamur Rahman

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

composer dumpautoload

Then seed the seeder

php artisan db:seed --class=CreateUsersTable

I hope it will work


composer dump-autoload did the trick for me after switching branches with diffrent talbe seeders
composer dump-autoload and composer dumpautoload works in same way
W
Wouter Van Damme

Try changing

  $this->call('UserTableSeeder');

to

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

and try running

 composer dump-autoload

S
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

u
user2511140

Add namespace in your class.

<?php

namespace Database\Seeders;
...

run composer dump-autoload after that


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now