I am working on a blogging application in Laravel 8.
The application gives the users roles and permissions. There is a many-to-many relationship between roles and permissions. For seeding the roles_permissions table, I have a the RolesPermissionsSeeder seeder:
class RolesPermissionsSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
RolesPermissions::create([
'role_id' => '1',
'permission_id' => '7',
]);
RolesPermissions::create([
'role_id' => '2',
'permission_id' => '1',
]);
RolesPermissions::create([
'role_id' => '2',
'permission_id' => '2',
]);
RolesPermissions::create([
'role_id' => '2',
'permission_id' => '3',
]);
}
}
The problem
As can be seen above, this chunk of code appears 3 times, with slight modifications:
RolesPermissions::create([
'role_id' => '2',
'permission_id' => '1',
]);
Using this instead did not work for me:
RolesPermissions::create([
'role_id' => '2',
'permission_id' => [1, 2, 3],
]);
Question
What is the easiest way to use one create statement for every role_id?
2
Answers
You can use
Or
Edit
To update without
updated_at
andcreated_at
userinsert()