gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 06:37:08 26 4
gpt4 key购买 nike

我正在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com