Skip to content

Commit

Permalink
Collection::only() with null returns all
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwinchester committed Sep 30, 2016
1 parent c6bd7c4 commit 91ace56
Show file tree
Hide file tree
Showing 4 changed files with 10 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 @@ -195,6 +195,10 @@ public function unique($key = null, $strict = false)
*/
public function only($keys)
{
if (is_null($keys)) {
return $this;
}

$dictionary = Arr::only($this->getDictionary(), $keys);

return new static(array_values($dictionary));
Expand Down
4 changes: 4 additions & 0 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,10 @@ public function min($callback = null)
*/
public function only($keys)
{
if (is_null($keys)) {
return $this;
}

$keys = is_array($keys) ? $keys : func_get_args();

return new static(Arr::only($this->items, $keys));
Expand Down
1 change: 1 addition & 0 deletions tests/Database/DatabaseEloquentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ public function testOnlyReturnsCollectionWithGivenModelKeys()

$c = new Collection([$one, $two, $three]);

$this->assertEquals($c, $c->only(null));
$this->assertEquals(new Collection([$one]), $c->only(1));
$this->assertEquals(new Collection([$two, $three]), $c->only([2, 3]));
}
Expand Down
1 change: 1 addition & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,7 @@ public function testOnly()
{
$data = new Collection(['first' => 'Taylor', 'last' => 'Otwell', 'email' => 'taylorotwell@gmail.com']);

$this->assertEquals($data->all(), $data->only(null)->all());
$this->assertEquals(['first' => 'Taylor'], $data->only(['first', 'missing'])->all());
$this->assertEquals(['first' => 'Taylor'], $data->only('first', 'missing')->all());

Expand Down

0 comments on commit 91ace56

Please sign in to comment.