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

[8.x] Preserve eloquent collection type after calling ->fresh() #34848

Merged
Merged
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
8 changes: 5 additions & 3 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,11 @@ public function fresh($with = [])
->get()
->getDictionary();

return $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()];
});
}

Expand Down
11 changes: 8 additions & 3 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1340,10 +1340,15 @@ public function testFreshMethodOnCollection()
EloquentTestUser::find(1)->update(['name' => 'Mathieu TUDISCO']);
EloquentTestUser::find(2)->update(['email' => '[email protected]']);

$this->assertEquals($users->map->fresh(), $users->fresh());
$this->assertCount(3, $users);
$this->assertNotEquals('Mathieu TUDISCO', $users[0]->name);
$this->assertNotEquals('[email protected]', $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('[email protected]', $refreshedUsers[1]->email);
}

public function testTimestampsUsingDefaultDateFormat()
Expand Down
8 changes: 6 additions & 2 deletions tests/Integration/Database/EloquentCollectionFreshTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Tests\Integration\Database\Fixtures\User;
Expand Down Expand Up @@ -30,8 +31,11 @@ public function testEloquentCollectionFresh()

$collection = User::all();

User::whereKey($collection->pluck('id')->toArray())->delete();
$collection->first()->delete();

$this->assertEmpty($collection->fresh()->filter());
$freshCollection = $collection->fresh();

$this->assertCount(1, $freshCollection);
$this->assertInstanceOf(EloquentCollection::class, $freshCollection);
}
}