Ir al contenido principal

Laravel Seeder Factory and Faked User Login

 laravel database seeder. why user::factory()- create(...) or user::create(...)?

Respuesta rápida

The primary difference lies in data generation strategy and automation.

User::create(...) is used for manual, static data insertion You must explicitly define every attribute value in the array. This is ideal for seeding specific, non-random records like an admin user or system configuration values where the data must be exact and predictable. 

User::factory()->create() is used for automated, random data generation It leverages the Faker library to generate realistic, varied fake data (names, emails, etc.) based on the blueprint defined in the UserFactory class.  This is the standard approach for populating databases with large amounts of test data or initial dummy records without writing repetitive code.

Key distinctions:

  • Control: create() offers full manual control over every field; factory() uses default definitions that can be partially overridden. 

  • Volume: create() is suited for small, specific sets; factory() is designed for bulk generation (e.g., User::factory(50)->create()). 

  • Testing: factory() is preferred for testing environments to ensure data variety and realism, whereas create() is better for initial setup or seed data that must remain constant.

Example usage in a seeder:

// Manual static user
User::create([
    'name' => 'Admin',
    'email' => 'admin@example.com',
]);

// Automated random users
User::factory()->count(10)->create();
What's the purpose of User::factory()->create([ 'name' => 'Test User', 'email' => 'test@example.com', ]);?

It overrides specific fields while letting the factory fill in everything else automatically.

In that example:

  • name and email are set to your exact values.

  • All other fields defined in UserFactory (e.g. passwordbcrypt('password'), remember_token, etc.) are generated by the factory as usual.

This is the sweet spot between the two approaches:

// ❌ Too manual — you must define every field
User::create([
    'name'    => 'Test User',
    'email'   => 'test@example.com',
    'password' => bcrypt('password'),
]);

// ❌ Too random — you can't control name/email
User::factory()->create();

// ✅ Factory handles the rest, you control what matters
User::factory()->create([
    'name'  => 'Test User',
    'email' => 'test@example.com',
]);

Typical use case: you need a known email (e.g. to assert against in tests or to log in as that user) but don't want to manually define the password or other fields the factory already handles.



How do I login with a user such as the test@example.com I showed?

It depends on the context:

In tests — use actingAs():

$user = User::factory()->create([
    'name'  => 'Test User',
    'email' => 'test@example.com',
]);

$response = $this->actingAs($user)->get('/dashboard');
$response->assertStatus(200);

This bypasses the login form entirely and sets the user as authenticated for the guard. 

In development (browser) — the default UserFactory sets the password to the plain string password (hashed internally via Hash::make('password')).  So you simply log in at your login page with:

  • Email: test@example.com

  • Password: password

If you need a different password, override it in the factory call:

User::factory()->create([
    'email'    => 'test@example.com',
    'password' => Hash::make('my-secret'),
]);

Then log in with my-secret

Comentarios