Skip to content

Commit a49b928

Browse files
committed
Factory returns fresh model instance on create
1 parent 3a833b3 commit a49b928

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/Illuminate/Database/Eloquent/FactoryBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function create(array $attributes = [])
153153
$this->store($results);
154154
}
155155

156-
return $results;
156+
return $results->fresh();
157157
}
158158

159159
/**

tests/Integration/Database/EloquentFactoryBuilderTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ public function creating_models_on_custom_connection()
180180
$this->assertTrue($user->is($dbUser));
181181
}
182182

183-
/** @test */
183+
/**
184+
* @test
185+
*/
184186
public function making_models_with_a_custom_connection()
185187
{
186188
$user = factory(FactoryBuildableUser::class)
@@ -189,6 +191,16 @@ public function making_models_with_a_custom_connection()
189191

190192
$this->assertEquals('alternative-connection', $user->getConnectionName());
191193
}
194+
195+
/**
196+
* @test
197+
*/
198+
public function creating_model_returns_fresh_instance()
199+
{
200+
$user = factory(FactoryBuildableUser::class)->create();
201+
202+
$this->assertEquals(1, $user->field_with_default);
203+
}
192204
}
193205

194206
class FactoryBuildableUser extends Model

0 commit comments

Comments
 (0)