Skip to content

Commit 7c00af9

Browse files
refactor: Use firstOrCreate for user seed/test data (#158)
Updates the user creation logic to use the Eloquent `firstOrCreate` method instead of `factory()->create`. This change achieves two main goals: 1. **Idempotency and Reliability:** Prevents the code from failing on repeated execution (e.g., when running seeders or tests multiple times) due to the database's unique constraint on the `email` column. `firstOrCreate` ensures the user is only created once. 2. **Complete User Data:** Includes essential attributes like a hashed password (`Hash::make('password')`) and sets `email_verified_at` to `now()`, making the test user fully functional for immediate authentication and verification checks.
1 parent 3d175f0 commit 7c00af9

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

database/seeders/DatabaseSeeder.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ public function run(): void
1515
{
1616
// User::factory(10)->create();
1717

18-
User::factory()->create([
19-
'name' => 'Test User',
20-
'email' => '[email protected]',
21-
]);
18+
User::firstOrCreate(
19+
['email' => '[email protected]'],
20+
[
21+
'name' => 'Test User',
22+
'password' => 'password',
23+
'email_verified_at' => now(),
24+
]
25+
);
2226
}
2327
}

0 commit comments

Comments
 (0)