Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

405
Views
Laravel reseeding a table

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!