Skip to content

Commit

Permalink
Fix for fresh on empty Collection (#19651)
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Isadov authored and taylorotwell committed Jun 18, 2017
1 parent 124213e commit 3a20ea2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ public function map(callable $callback)
*/
public function fresh($with = [])
{
if ($this->isEmpty()) {
return new static;
}

$model = $this->first();

$freshModels = $model->newQueryWithoutScopes()
Expand Down
4 changes: 4 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use PHPUnit\Framework\TestCase;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\Model as Eloquent;
Expand Down Expand Up @@ -1109,6 +1110,9 @@ public function testFreshMethodOnCollection()
EloquentTestUser::find(2)->update(['email' => 'dev@mathieutu.ovh']);

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

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

/**
Expand Down

0 comments on commit 3a20ea2

Please sign in to comment.