2

In Laravel how would you reseed a database? Without losing existing data / migrations?

I have a RoleAndPermissionSeeder.php with Spatie's Permission 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?

apokryfos
  • 38,771
  • 9
  • 70
  • 114
Ronald
  • 69
  • 8
  • 1
    Read [Running Seeders](https://laravel.com/docs/8.x/seeding#running-seeders), "_you may use the --class option to specify a specific seeder class to run individually_" – brombeer Feb 02 '22 at 13:50
  • @brombeer thanks for your reply, I have seen and read the documentation on seeders but when I try to reseed it will throw an error saying the data already exists. Or would you, just like a migration create a new seeder for these occasions? – Ronald Feb 02 '22 at 13:58
  • @Ronald I would think you should modify your seeder to check what data is already there and then only insert the missing ones – apokryfos Feb 02 '22 at 15:06
  • You could use `firstOrCreate` instead of `create` in your seeder – IGP Feb 02 '22 at 16:24

1 Answers1

3

If your seeder class has firstOrCreate instead of 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.

As long as you don't drop the table, you shouldn't lose any data.

IGP
  • 14,160
  • 4
  • 26
  • 43