From b0df1ff0e741f7046f2f793e68c223575d437400 Mon Sep 17 00:00:00 2001 From: Caleb Porzio Date: Thu, 15 Oct 2020 10:40:53 -0400 Subject: [PATCH 1/4] Preserve eloquent collection class type after calling ->fresh() --- src/Illuminate/Database/Eloquent/Collection.php | 4 ++-- tests/Integration/Database/EloquentCollectionFreshTest.php | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Collection.php b/src/Illuminate/Database/Eloquent/Collection.php index 70c90e97ce92..ec0de3399481 100755 --- a/src/Illuminate/Database/Eloquent/Collection.php +++ b/src/Illuminate/Database/Eloquent/Collection.php @@ -300,10 +300,10 @@ public function fresh($with = []) ->get() ->getDictionary(); - return $this->map(function ($model) use ($freshModels) { + return new static($this->map(function ($model) use ($freshModels) { return $model->exists && isset($freshModels[$model->getKey()]) ? $freshModels[$model->getKey()] : null; - }); + })); } /** diff --git a/tests/Integration/Database/EloquentCollectionFreshTest.php b/tests/Integration/Database/EloquentCollectionFreshTest.php index 745be7ae3074..640779bd69da 100644 --- a/tests/Integration/Database/EloquentCollectionFreshTest.php +++ b/tests/Integration/Database/EloquentCollectionFreshTest.php @@ -3,6 +3,7 @@ namespace Illuminate\Tests\Integration\Database; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Support\Facades\Schema; use Illuminate\Tests\Integration\Database\Fixtures\User; @@ -32,6 +33,9 @@ public function testEloquentCollectionFresh() User::whereKey($collection->pluck('id')->toArray())->delete(); - $this->assertEmpty($collection->fresh()->filter()); + $freshCollection = $collection->fresh(); + + $this->assertEmpty($freshCollection->filter()); + $this->assertInstanceOf(EloquentCollection::class, $freshCollection); } } From ba926a791601f8ff9c259a3f26d46495fc6baec9 Mon Sep 17 00:00:00 2001 From: Caleb Porzio Date: Thu, 15 Oct 2020 10:48:59 -0400 Subject: [PATCH 2/4] Re-order imports --- tests/Integration/Database/EloquentCollectionFreshTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Database/EloquentCollectionFreshTest.php b/tests/Integration/Database/EloquentCollectionFreshTest.php index 640779bd69da..7a762850b97c 100644 --- a/tests/Integration/Database/EloquentCollectionFreshTest.php +++ b/tests/Integration/Database/EloquentCollectionFreshTest.php @@ -2,8 +2,8 @@ namespace Illuminate\Tests\Integration\Database; -use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Eloquent\Collection as EloquentCollection; +use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; use Illuminate\Tests\Integration\Database\Fixtures\User; From f67c0ad1f92ab81b8e2ae44b835d9c15da070dba Mon Sep 17 00:00:00 2001 From: Caleb Porzio Date: Fri, 16 Oct 2020 10:34:50 -0400 Subject: [PATCH 3/4] Update ->fresh() to filter out non-existant models --- src/Illuminate/Database/Eloquent/Collection.php | 10 ++++++---- .../Database/EloquentCollectionFreshTest.php | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Collection.php b/src/Illuminate/Database/Eloquent/Collection.php index ec0de3399481..eafe059473f9 100755 --- a/src/Illuminate/Database/Eloquent/Collection.php +++ b/src/Illuminate/Database/Eloquent/Collection.php @@ -300,10 +300,12 @@ public function fresh($with = []) ->get() ->getDictionary(); - return new static($this->map(function ($model) use ($freshModels) { - return $model->exists && isset($freshModels[$model->getKey()]) - ? $freshModels[$model->getKey()] : null; - })); + return $this->filter(function ($model) use ($freshModels) { + return $model->exists && isset($freshModels[$model->getKey()]); + }) + ->map(function ($model) use ($freshModels) { + return $freshModels[$model->getKey()]; + }); } /** diff --git a/tests/Integration/Database/EloquentCollectionFreshTest.php b/tests/Integration/Database/EloquentCollectionFreshTest.php index 7a762850b97c..b0f0e0983873 100644 --- a/tests/Integration/Database/EloquentCollectionFreshTest.php +++ b/tests/Integration/Database/EloquentCollectionFreshTest.php @@ -31,11 +31,11 @@ public function testEloquentCollectionFresh() $collection = User::all(); - User::whereKey($collection->pluck('id')->toArray())->delete(); + $collection->first()->delete(); $freshCollection = $collection->fresh(); - $this->assertEmpty($freshCollection->filter()); + $this->assertCount(1, $freshCollection); $this->assertInstanceOf(EloquentCollection::class, $freshCollection); } } From 27434ff2d362fa5c412f8d39ecdbeed38af16ab7 Mon Sep 17 00:00:00 2001 From: Caleb Porzio Date: Fri, 16 Oct 2020 10:45:15 -0400 Subject: [PATCH 4/4] Fix ->fresh() test --- tests/Database/DatabaseEloquentIntegrationTest.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/Database/DatabaseEloquentIntegrationTest.php b/tests/Database/DatabaseEloquentIntegrationTest.php index a5273c26dd0c..55e413a05096 100644 --- a/tests/Database/DatabaseEloquentIntegrationTest.php +++ b/tests/Database/DatabaseEloquentIntegrationTest.php @@ -1340,10 +1340,15 @@ public function testFreshMethodOnCollection() EloquentTestUser::find(1)->update(['name' => 'Mathieu TUDISCO']); EloquentTestUser::find(2)->update(['email' => 'dev@mathieutu.ovh']); - $this->assertEquals($users->map->fresh(), $users->fresh()); + $this->assertCount(3, $users); + $this->assertNotEquals('Mathieu TUDISCO', $users[0]->name); + $this->assertNotEquals('dev@mathieutu.ovh', $users[1]->email); - $users = new Collection; - $this->assertEquals($users->map->fresh(), $users->fresh()); + $refreshedUsers = $users->fresh(); + + $this->assertCount(2, $refreshedUsers); + $this->assertEquals('Mathieu TUDISCO', $refreshedUsers[0]->name); + $this->assertEquals('dev@mathieutu.ovh', $refreshedUsers[1]->email); } public function testTimestampsUsingDefaultDateFormat()