I have a table called permissions
; this is like the different roles a user can have. As the project grows, I may add more functionalities, therefore more permissions
are created.
I have a PermissionSeeder.php
that I would like to keep on adding the permissions that I add. Then every time a new permission is added, I would like to run the seeder on it. But, the existing entries should not be created.
I was thinking of truncating the table on every update, but then permission_id
on the other pivot tables will conflict since they are the foreign keys.
This is what I was going to do at the moment:
use Permission;
class PermissionSeeder extends Seeder {
public function run()
{
$permissions = [...]; // this is the array of ALL permissions that will keep on growing
foreach ($permissions as $permission) {
$model = Permission::whereName($permission['name']);
if ($model->count() <= 0) {
DB::table('permissions')->insert($permission);
}
}
}
}
Is there a better practice?