php - Laravel 播种我的表(多对多关系)失败

标签 php mysql laravel

我正在 Laravel 中构建一个身份验证系统,用户可以在其中拥有不同的角色。我有三张 table 。 用户角色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    |                |
+---------+------------------+------+-----+---------+----------------+

我想为我的表播种:

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

}

关于我的问题:如何为 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 在您的用户表中是一对多关系,而不是多对多关系,如数据透视表。

为了给 John 分配 super 管理员的角色:

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

关于php - Laravel 播种我的表(多对多关系)失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25431374/

相关文章:

javascript - 在 Cors 中发送原始 cookie 不适用于 VideoJS

php - MySQL 和 PHP : Display matrix table dynamically

mysql - 具有树状数据的表的数据库规范化

php - mysql中的多重求和

mysql - Amazon RDS CPU 峰值

laravel - GuzzleHttp客户端:CURL error 3: <url> malformed

php - 使用 KEY/CONSTRAINT 与 FOREIGN KEY 之间的区别?

javascript - 通过 Ajax 调用特定类中的 PHP 函数

php - 在这种情况下,如何从具有不同 where 条件和不同 group by 条件的同一个表中进行选择?

php - 两个属性与一个实体共享相同的 OneToMany 关系 Symfony2