Skip to content

Commit

Permalink
Factory returns fresh model instance on create
Browse files Browse the repository at this point in the history
  • Loading branch information
dallincoons committed Sep 23, 2017
1 parent 3a833b3 commit 9be2328
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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

0 comments on commit 9be2328

Please sign in to comment.