获取Container.php第752行:当尝试在Laravel中播种角色时,类RoleTableSeeder不存在错误

时间:2018-02-05 11:56:10

标签: php laravel seed

目前,我正在尝试为我的应用程序创建角色,不幸的是我遇到了一些麻烦。每当我运行php artisan migrate --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');
    }
}

用户表:

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();
        });
    }

角色表:

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表

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

3 个答案:

答案 0 :(得分:2)

如评论中所述:

运行:composer dump-autoload

如果引发Composer\Exception\NoSslException异常,则可能需要在composer config -g -- disable-tls true之前运行composer dump-autoload

答案 1 :(得分:0)

我遇到了与上述相同的问题 composer命令无法解决问题。

我在Laravel 5.5上跑了

 php artisan cache:clear

,然后找到我所有新创建的种子类

答案 2 :(得分:0)

在Lavarel 5.5。*上,我在PH 7.2。*上也遇到了同样的问题。

解决方案非常容易-使用名称空间:

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

也在这里:

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