I am building an authentication system in Laravel where a user can have different roles. I have three tables. users
, roles
, role_user
and
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 | |
+---------+------------------+------+-----+---------+----------------+
I want to seed my tables:
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'),
));
}
}
To my question: How do i seed the role_user table? Do i need a model for that? With user and role table i have the model User and Role.
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');
}
}
The fail i get when i try seed is:
Argument 2 passed to Illuminate\Database\Schema\Builder::table() must be an instance of Closure, none given
Two issues with your code. First of all your error is because of this:
Schema::table('roles')->delete();
Schema
class is only used for the schema builder, which is already done at this point. You don't need to use the Schema
class here. Instead use the DB
class.
DB::table('roles')->delete();
The next problem, no where in your code are you actually assigning roles. The role_id
in your user table is pointless, since you are supposed to be assigning roles using a pivot table.
role_id
in your users table in a one-to-many relationship, not many-to-many, like the pivot table.
In order to assign John the role of 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' => '[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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With