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

时间:2014-08-21 16:30:19

标签: php mysql laravel

我正在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'    => '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

3 个答案:

答案 0 :(得分:5)

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

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

Schema类仅用于架构构建器,此时已完成。您不需要在此处使用Schema课程。而是使用DB类。

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

下一个问题是,您的代码中没有实际分配角色的位置。用户表中的role_id毫无意义,因为您应该使用数据透视表分配角色。

您的用户表中的

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

答案 1 :(得分:2)

您需要像我们建议的那样,将Schema::table更正为DB::table

然而,种子数据透视表的最简单,最高效的方法是这样的:

$users = User::listst('id');

$roles = Role::lists('id');

$usersRoles = [
   // assign roles to users however you like - manually
];

// or maybe in a foreach look

// then run single insert
DB::table('role_user')->insert($usresRoles);

答案 2 :(得分:0)

它出现在错误消息中。 Schema::table()期望这样的事情:

Schema::table('table_name', function($table)
{
    // actions inside of the closure function
});

你有:

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

您正在寻找的是DB::table('roles')->delete()DB::table('roles')->truncate()功能:

// performs a delete, safe for use with foreign keys
// you can use truncate where you don't have to worry about foreign keys
DB::table('roles')->delete();

更多信息可以是found in the docs section regarding record deletion