diff --git a/src/Illuminate/Database/Eloquent/Collection.php b/src/Illuminate/Database/Eloquent/Collection.php index 16fb1a484278..45c80ee92724 100755 --- a/src/Illuminate/Database/Eloquent/Collection.php +++ b/src/Illuminate/Database/Eloquent/Collection.php @@ -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) { @@ -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. * diff --git a/tests/Database/DatabaseEloquentIntegrationTest.php b/tests/Database/DatabaseEloquentIntegrationTest.php index 028b149572f5..c72960e4ba27 100644 --- a/tests/Database/DatabaseEloquentIntegrationTest.php +++ b/tests/Database/DatabaseEloquentIntegrationTest.php @@ -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' => 'taylorotwell@gmail.com']); + $storedUser1->newQuery()->update(['email' => 'dev@mathieutu.ovh', 'name' => 'Mathieu TUDISCO']); + $freshStoredUser1 = $storedUser1->fresh(); + + $storedUser2 = EloquentTestUser::create(['id' => 2, 'email' => 'taylorotwell@gmail.com']); + $storedUser2->newQuery()->update(['email' => 'dev@mathieutu.ovh']); + $freshStoredUser2 = $storedUser2->fresh(); + + $notStoredUser = new EloquentTestUser(['id' => 3, 'email' => 'taylorotwell@gmail.com']); + $freshNotStoredUser = $notStoredUser->fresh(); + + $this->assertEquals(['id' => 1, 'email' => 'taylorotwell@gmail.com', 'created_at' => $now, 'updated_at' => $now], $storedUser1->toArray()); + $this->assertEquals(['id' => 1, 'name' => 'Mathieu TUDISCO', 'email' => 'dev@mathieutu.ovh', 'created_at' => $now, 'updated_at' => $now], $freshStoredUser1->toArray()); + $this->assertInstanceOf(EloquentTestUser::class, $storedUser1); + + $this->assertEquals(['id' => 2, 'email' => 'taylorotwell@gmail.com', 'created_at' => $now, 'updated_at' => $now], $storedUser2->toArray()); + $this->assertEquals(['id' => 2, 'name' => null, 'email' => 'dev@mathieutu.ovh', 'created_at' => $now, 'updated_at' => $now], $freshStoredUser2->toArray()); + $this->assertInstanceOf(EloquentTestUser::class, $storedUser2); + + $this->assertEquals(['id' => 3, 'email' => 'taylorotwell@gmail.com'], $notStoredUser->toArray()); + $this->assertEquals(null, $freshNotStoredUser); + } + + public function testFreshMethodOnCollection() + { + EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']); + EloquentTestUser::create(['id' => 2, 'email' => 'taylorotwell@gmail.com']); + + $users = EloquentTestUser::all() + ->add(new EloquentTestUser(['id' => 3, 'email' => 'taylorotwell@gmail.com'])); + + EloquentTestUser::find(1)->update(['name' => 'Mathieu TUDISCO']); + EloquentTestUser::find(2)->update(['email' => 'dev@mathieutu.ovh']); + + $this->assertEquals($users->map->fresh(), $users->fresh()); + } + /** * Helpers... */