进入 Container.php 行 752:Class RoleTableSeeder 不存在错误,当试图在 Laravel 中播种角色时

Getting In Container.php line 752: Class RoleTableSeeder does not exist error, when trying to seed a role in Laravel

目前,我正在尝试为我的应用程序创建角色,不幸的是我遇到了一些麻烦。每当我 运行 php artisan 迁移 --seed 时,我都会收到我在标题中写的错误。老实说,我觉得我搞砸了一些非常简单的东西,比如名字,但我就是找不到我的错误。如果有任何帮助,我将不胜感激。

User.php 型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;


class User extends Model implements Authenticatable
{
    public function roles(){
        return $this->belongsToMany('App\Role');
    }
}

Role.php 型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    public function users(){
        return $this->belongsToMany('App\User');
    }
}

用户table:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('first_name');
            $table->string('last_name');
            $table->string('username');
            $table->string('password');
            $table->string('email');
            $table->timestamps();
            $table->rememberToken();
        });
    }

角色table:

public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('description')->nullable()->default(null);
            $table->timestamps();
        });
    }

role_user table

public function up()
{
    Schema::create('role_user', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id');
        $table->integer('role_id');
        $table->timestamps();
    });
}

RoleTableSeeder.php

    <?php

use Illuminate\Database\Seeder;
use App\Role;

class RoleTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $role_user = new Role();
        $role_user->name = 'User';
        $role_user->description = "Normal User";
        $role_user->save();

        $role_admin = new Role();
        $role_admin->name = 'Admin';
        $role_admin->description = "Admin User";
        $role_admin->save();
    }
}

UserTableSeeder.php

public function run()
    {
        $role_admin = Role::where('name', 'Admin')->first();

        $user = new User();
        $user->first_name = 'test';
        $user->last_name = 'test';
        $user->username = 'Admin';
        $user->password = bcrypt('test');
        $user->email = 'test@gmail.com';
        $user->save();
        $user->roles()->attach($role_admin);
    }

DatabaseSeeder.php

public function run()
    {
        $this->call(RoleTableSeeder::class);
        $this->call(UserTableSeeder::class);
    }
}

如评论中所述:

运行: composer dump-autoload.

如果抛出Composer\Exception\NoSslException异常,可能需要在composer dump-autoload.

之前运行composer config -g -- disable-tls true

我遇到了和上面一样的问题 composer 命令没有成功。

在 Laravel 5.5 我 运行

 php artisan cache:clear

然后我所有新创建的播种机 类 都被发现了

我在 PH 7.2.* 和 Laravel 5.5.* 上遇到了同样的问题。*

解决方案非常简单 - 使用命名空间:

php artisan db:seed --class=Modules\{moduleName}\Database\Seeders\{className}DatabaseSeeder

也在这里:

$this->call('Modules\{moduleName}\Database\Seeders\{className}TableSeeder');