Skip to content
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
4 changes: 2 additions & 2 deletions src/Illuminate/Http/Concerns/InteractsWithInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,12 @@ public function boolean($key = null, $default = false)
/**
* Retrieve input from the request as a collection.
*
* @param string|null $key
* @param array|string|null $key
* @return \Illuminate\Support\Collection
*/
public function collect($key = null)
{
return collect($this->input($key));
return collect(is_array($key) ? $this->only($key) : $this->input($key));
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,15 @@ public function testCollectMethod()
$request = Request::create('/', 'GET', []);
$this->assertInstanceOf(Collection::class, $request->collect());
$this->assertTrue($request->collect()->isEmpty());

$request = Request::create('/', 'GET', ['users' => [1, 2, 3], 'roles' => [4, 5, 6], 'foo' => ['bar', 'baz'], 'email' => 'test@example.com']);
$this->assertInstanceOf(Collection::class, $request->collect(['users']));
$this->assertTrue($request->collect(['developers'])->isEmpty());
$this->assertTrue($request->collect(['roles'])->isNotEmpty());
$this->assertEquals(['roles' => [4, 5, 6]], $request->collect(['roles'])->all());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're only ever passing one key into the collect() method on your test. It would be better to pass multiple keys in to really test that we're able to grab more than one key with your addition of only(). Expand your initial request to 3-4 parameters.

$this->assertEquals(['users' => [1, 2, 3], 'email' => 'test@example.com'], $request->collect(['users', 'email'])->all());
$this->assertEquals(collect(['roles' => [4, 5, 6], 'foo' => ['bar', 'baz']]), $request->collect(['roles', 'foo']));
$this->assertEquals(['users' => [1, 2, 3], 'roles' => [4, 5, 6], 'foo' => ['bar', 'baz'], 'email' => 'test@example.com'], $request->collect()->all());
}

public function testArrayAccess()
Expand Down