Skip to content

Commit

Permalink
Collection::only with null or '*' 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 fc3f758
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,12 @@ public function unique($key = null, $strict = false)
* @param mixed $keys
* @return static
*/
public function only($keys)
public function only($keys = '*')
{
if (is_null($keys) || $keys === '*') {
return $this;
}

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

return new static(array_values($dictionary));
Expand Down
6 changes: 5 additions & 1 deletion src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,12 @@ public function min($callback = null)
* @param mixed $keys
* @return static
*/
public function only($keys)
public function only($keys = '*')
{
if (is_null($keys) || $keys === '*') {
return $this;
}

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

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

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

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

$this->assertEquals($data->all(), $data->only()->all());
$this->assertEquals($data->all(), $data->only(null)->all());
$this->assertEquals($data->all(), $data->only('*')->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 fc3f758

Please sign in to comment.