进入 Container.php 第 752 行:尝试在 Laravel 中播种角色时,类 RoleTableSeeder 不存在错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48622071/
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
Getting In Container.php line 752: Class RoleTableSeeder does not exist error, when trying to seed a role in Laravel
提问by Bobimaru
Currently, I am trying to create roles for my application, unfortunately I am having some troubles. Whenever I run php artisan migrate --seed, I get the error I've written in the title. Honestly, I feel like I've messed up something really simple like a name but I just can't find my mistake. I'd appreciate any help.
目前,我正在尝试为我的应用程序创建角色,不幸的是我遇到了一些麻烦。每当我运行 php artisan migrate --seed 时,我都会收到标题中写的错误。老实说,我觉得我把名字这样简单的东西搞砸了,但我就是找不到我的错误。我很感激任何帮助。
User.php model:
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 model:
Role.php 模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
public function users(){
return $this->belongsToMany('App\User');
}
}
Users 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();
});
}
Roles 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
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
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
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 = '[email protected]';
$user->save();
$user->roles()->attach($role_admin);
}
DatabaseSeeder.php
数据库浏览器.php
public function run()
{
$this->call(RoleTableSeeder::class);
$this->call(UserTableSeeder::class);
}
}
回答by Amade
As mentioned in the comments:
正如评论中提到的:
Run: composer dump-autoload
.
运行:composer dump-autoload
。
If Composer\Exception\NoSslException
exception is thrown, you may need to run composer config -g -- disable-tls true
before composer dump-autoload
.
如果Composer\Exception\NoSslException
抛出异常,您可能需要composer config -g -- disable-tls true
在composer dump-autoload
.
回答by Edwin Krause
I had the same issue as above the composer command didn't do the trick.
我遇到了与上面相同的问题,composer 命令没有解决问题。
on Laravel 5.5 I ran
在 Laravel 5.5 上我跑了
php artisan cache:clear
and then all my new created seeder classes were found
然后找到了我所有新创建的播种机类
回答by Enot
I have faced the same issue on PH 7.2.* with Lavarel 5.5.*
我在 PH 7.2.* 和 Lavarel 5.5.* 上遇到了同样的问题
Solution very easy - use the namespace:
解决方案非常简单 - 使用命名空间:
php artisan db:seed --class=Modules\{moduleName}\Database\Seeders\{className}DatabaseSeeder
Also here:
也在这里:
$this->call('Modules\{moduleName}\Database\Seeders\{className}TableSeeder');