[英]Laravel reseeding a table
In Laravel how would you reseed a database?在 Laravel 中,您将如何重新植入数据库? Without losing existing data / migrations?
不丢失现有数据/迁移?
I have a RoleAndPermissionSeeder.php with Spatie's Permission package:我有一个 RoleAndPermissionSeeder.php 和 Spatie 的许可 package:
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;
class RoleAndPermissionSeeder extends Seeder
{
public function run()
{
// Reset cached roles and permissions
app()[PermissionRegistrar::class]->forgetCachedPermissions();
$permissions = [
'articles create',
'articles delete',
'articles read',
'articles update',
'dashboard read',
];
foreach ($permissions as $permission) {
Permission::create(['name' => $permission]);
}
// =======================================================================
$admin = Role::create(['name' => 'admin']);
$member = Role::create(['name' => 'member']);
Role::create(['name' => 'super-admin']);
// =======================================================================
$admin_permissions = [
'articles read',
'dashboard read',
];
$member_permissions = [
'dashboard read',
];
$admin->syncPermissions($admin_permissions);
$member->syncPermissions($member_permissions);
}
}
After creating a new migration for a blog I have added extra permissions to read, update the blog and so on.在为博客创建新迁移后,我添加了额外的阅读、更新博客等权限。 How would I reseed this file so that the new permissions get added?
我将如何重新设置此文件以添加新权限?
If your seeder class has firstOrCreate
instead of create
如果您的播种机 class 具有
firstOrCreate
而不是create
foreach ($permissions as $permission) {
Permission::firstOrCreate(['name' => $permission]);
}
// =======================================================================
$admin = Role::firstOrCreate(['name' => 'admin']);
$member = Role::firstOrCreate(['name' => 'member']);
Role::firstOrCreate(['name' => 'super-admin']);
It will not duplicate the data.它不会复制数据。 If
syncPermissions
uses sync
behind the scenes, then it shouldn't be a problem.如果
syncPermissions
在幕后使用sync
,那么它应该不是问题。
As long as you don't drop the table, you shouldn't lose any data.只要不丢弃表,就不会丢失任何数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.