Laravel播种我的桌子(多对多关系)失败

Bul*_*nch 7 php mysql laravel

我正在Laravel中构建一个身份验证系统,用户可以在其中拥有不同的角色.我有三张桌子.users,roles,role_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    |                |
+---------+------------------+------+-----+---------+----------------+
Run Code Online (Sandbox Code Playgroud)

我想种下我的桌子:

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'    => 'john.svensson@gmail.com',
            'role_id'   => 1,
            'password' => Hash::make('1234'),
        ));
        User::create(array(
            'name'     => 'Carl',
            'surname'     => 'Svensson',
            'username' => 'Calle S',
            'email'    => 'carl.svensson@gmail.com',
            'role_id'   => 2,
            'password' => Hash::make('1111'),
        ));
    }

}
Run Code Online (Sandbox Code Playgroud)

我的问题:我如何播种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');
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试种子时遇到的失败是:

Argument 2 passed to Illuminate\Database\Schema\Builder::table() must be an instance of Closure, none given
Run Code Online (Sandbox Code Playgroud)

Jak*_*son 5

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

Schema::table('roles')->delete();
Run Code Online (Sandbox Code Playgroud)

Schemaclass仅用于模式构建器,此时已经完成.你不需要在Schema这里使用这个类.而是使用DB该类.

DB::table('roles')->delete();
Run Code Online (Sandbox Code Playgroud)

下一个问题是,您的代码中没有实际分配角色的位置.将role_id在user表是没有意义的,因为你应该被分配使用数据透视表的作用.

role_id 在您的用户表中以一对多关系,而不是多对多,如数据透视表.

为了给约翰分配Superadmin的角色:

// Create the role
$superadmin = Role::create(array(
    'role'     => 'Superadmin'
));


// Create the user
$user = User::create(array(
    'name'     => 'John',
    'surname'     => 'Svensson',
    'username' => 'John_superadmin',
    'email'    => 'john.svensson@gmail.com',
    '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);
Run Code Online (Sandbox Code Playgroud)