Skip to content

[5.5] Factory returns fresh model instance on create #21354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/FactoryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function create(array $attributes = [])
$this->store($results);
}

return $results;
return $results->fresh();
}

/**
Expand Down
15 changes: 14 additions & 1 deletion tests/Integration/Database/EloquentFactoryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function setUp()
$table->increments('id');
$table->string('name');
$table->string('email');
$table->boolean('field_with_default')->default(1);
});

Schema::connection('alternative-connection')->create('users', function ($table) {
Expand Down Expand Up @@ -180,7 +181,9 @@ public function creating_models_on_custom_connection()
$this->assertTrue($user->is($dbUser));
}

/** @test */
/**
* @test
*/
public function making_models_with_a_custom_connection()
{
$user = factory(FactoryBuildableUser::class)
Expand All @@ -189,6 +192,16 @@ public function making_models_with_a_custom_connection()

$this->assertEquals('alternative-connection', $user->getConnectionName());
}

/**
* @test
*/
public function creating_model_returns_fresh_instance()
{
$user = factory(FactoryBuildableUser::class)->create();

$this->assertEquals(1, $user->field_with_default);
}
}

class FactoryBuildableUser extends Model
Expand Down