Skip to content
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

[5.4] Factory: refresh model(s) from database when created. #19621

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
23 changes: 22 additions & 1 deletion src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function merge($items)
* Run a map over each of the items.
*
* @param callable $callback
* @return \Illuminate\Support\Collection
* @return \Illuminate\Support\Collection|static
*/
public function map(callable $callback)
{
Expand All @@ -138,6 +138,27 @@ public function map(callable $callback)
}) ? $result->toBase() : $result;
}

/**
* Reload a fresh model instance from the database for all the entities.
*
* @param array|string $with
* @return static
*/
public function fresh($with = [])
{
$model = $this->first();

$freshModels = $model->newQueryWithoutScopes()
->with(is_string($with) ? func_get_args() : $with)
->whereIn($model->getKeyName(), $this->modelKeys())
->get()
->getDictionary();

return $this->map(function ($model) use ($freshModels) {
return $model->exists ? $freshModels[$model->getKey()] : null;
});
}

/**
* Diff the collection with the given items.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/FactoryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function create(array $attributes = [])
$this->store($results);
}

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

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,48 @@ public function testIsAfterRetrievingTheSameModel()
$this->assertTrue($saved->is($retrieved));
}

public function testFreshMethodOnModel()
{
$now = \Carbon\Carbon::now();
\Carbon\Carbon::setTestNow($now);

$storedUser1 = EloquentTestUser::create(['id' => 1, 'email' => '[email protected]']);
$storedUser1->newQuery()->update(['email' => '[email protected]', 'name' => 'Mathieu TUDISCO']);
$freshStoredUser1 = $storedUser1->fresh();

$storedUser2 = EloquentTestUser::create(['id' => 2, 'email' => '[email protected]']);
$storedUser2->newQuery()->update(['email' => '[email protected]']);
$freshStoredUser2 = $storedUser2->fresh();

$notStoredUser = new EloquentTestUser(['id' => 3, 'email' => '[email protected]']);
$freshNotStoredUser = $notStoredUser->fresh();

$this->assertEquals(['id' => 1, 'email' => '[email protected]', 'created_at' => $now, 'updated_at' => $now], $storedUser1->toArray());
$this->assertEquals(['id' => 1, 'name' => 'Mathieu TUDISCO', 'email' => '[email protected]', 'created_at' => $now, 'updated_at' => $now], $freshStoredUser1->toArray());
$this->assertInstanceOf(EloquentTestUser::class, $storedUser1);

$this->assertEquals(['id' => 2, 'email' => '[email protected]', 'created_at' => $now, 'updated_at' => $now], $storedUser2->toArray());
$this->assertEquals(['id' => 2, 'name' => null, 'email' => '[email protected]', 'created_at' => $now, 'updated_at' => $now], $freshStoredUser2->toArray());
$this->assertInstanceOf(EloquentTestUser::class, $storedUser2);

$this->assertEquals(['id' => 3, 'email' => '[email protected]'], $notStoredUser->toArray());
$this->assertEquals(null, $freshNotStoredUser);
}

public function testFreshMethodOnCollection()
{
EloquentTestUser::create(['id' => 1, 'email' => '[email protected]']);
EloquentTestUser::create(['id' => 2, 'email' => '[email protected]']);

$users = EloquentTestUser::all()
->add(new EloquentTestUser(['id' => 3, 'email' => '[email protected]']));

EloquentTestUser::find(1)->update(['name' => 'Mathieu TUDISCO']);
EloquentTestUser::find(2)->update(['email' => '[email protected]']);

$this->assertEquals($users->map->fresh(), $users->fresh());
}

/**
* Helpers...
*/
Expand Down