我正在laravel中构建一个身份验证系统,用户可以具有不同的角色。我有三张桌子。usersrolesrole_user

users
+----------------+------------------+------+-----+---------------------+----------------+
| Field          | Type             | Null | Key | Default             | Extra          |
+----------------+------------------+------+-----+---------------------+----------------+
| id             | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| username       | varchar(255)     | NO   |     | NULL                |                |
| name           | varchar(255)     | NO   |     | NULL                |                |
| surname        | varchar(255)     | NO   |     | NULL                |                |
| email          | varchar(255)     | NO   |     | NULL                |                |
| role_id        | int(10) unsigned | NO   | MUL | NULL                |                |
| password       | varchar(255)     | NO   |     | NULL                |                |
| remember_token | varchar(100)     | YES  |     | NULL                |                |
| created_at     | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at     | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+----------------+------------------+------+-----+---------------------+----------------+

roles
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| role  | varchar(255)     | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+

role_user
+---------+------------------+------+-----+---------+----------------+
| Field   | Type             | Null | Key | Default | Extra          |
+---------+------------------+------+-----+---------+----------------+
| id      | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| role_id | int(10) unsigned | NO   | MUL | NULL    |                |
| user_id | int(10) unsigned | NO   | MUL | NULL    |                |
+---------+------------------+------+-----+---------+----------------+

我想为我的桌子播种:
class UserTableSeeder extends Seeder
{

    public function run()
    {
        Schema::table('roles')->delete();
        Role::create(array(
            'role'     => 'Superadmin'
        ));
        Role::create(array(
            'role'     => 'Admin'
        ));
        Role::create(array(
            'role'     => 'User'
        ));

        Schema::table('users')->delete();
        User::create(array(
            'name'     => 'John',
            'surname'     => 'Svensson',
            'username' => 'John_superadmin',
            'email'    => '[email protected]',
            'role_id'   => 1,
            'password' => Hash::make('1234'),
        ));
        User::create(array(
            'name'     => 'Carl',
            'surname'     => 'Svensson',
            'username' => 'Calle S',
            'email'    => '[email protected]',
            'role_id'   => 2,
            'password' => Hash::make('1111'),
        ));
    }

}

对于我的问题:如何为role_user表种子?我需要一个模型吗?有了用户和角色表,我就有了模型用户和角色。
class Role extends Eloquent{

    public function users()
    {
        return $this->belongsToMany('User');
    }

}

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');

    public function roles()
    {
        return $this->belongsToMany('Role');
    }
}

当我尝试播种时,失败的是:
Argument 2 passed to Illuminate\Database\Schema\Builder::table() must be an instance of Closure, none given

最佳答案

代码有两个问题。首先,您的错误是因为:

Schema::table('roles')->delete();

Schema类仅用于架构生成器,此时已完成。您不需要使用这里的Schema类。而是使用DB类。
DB::table('roles')->delete();

下一个问题是,代码中没有实际分配角色的位置。用户表中的role_id是没有意义的,因为您应该使用透视表分配角色。
role_id在一对多关系中的用户表中,不是像透视表那样的多对多关系。
为了让约翰扮演超级管理员的角色:
// Create the role
$superadmin = Role::create(array(
    'role'     => 'Superadmin'
));


// Create the user
$user = User::create(array(
    'name'     => 'John',
    'surname'     => 'Svensson',
    'username' => 'John_superadmin',
    'email'    => '[email protected]',
    'password' => Hash::make('1234'),
));

// Assign roles using one of several methods:

// Syncing
$user->roles()->sync([$superadmin->id]);

// or attaching
$user->roles()->attach($superadmin->id);

// or save
$user->roles()->save($superadmin);

07-24 16:49