Skip to content

Commit

Permalink
Check if collection is empty and prevent exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieutu committed Jun 19, 2017
1 parent 7cd6ae2 commit 0d58b25
Show file tree
Hide file tree
Showing 2 changed files with 11 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
7 changes: 7 additions & 0 deletions tests/Database/DatabaseEloquentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@ public function testQueueableCollectionImplementationThrowsExceptionOnMultipleMo
$c = new Collection([new TestEloquentCollectionModel, (object) ['id' => 'something']]);
$c->getQueueableClass();
}

public function testEmptyCollectionStayEmptyOnFresh()
{
$c = new Collection();
$this->assertEquals($c, $c->fresh());

}
}

class TestEloquentCollectionModel extends \Illuminate\Database\Eloquent\Model
Expand Down

0 comments on commit 0d58b25

Please sign in to comment.