Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Subset in request's collect #39191

Merged
merged 4 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,13 @@ 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]]);
$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], 'roles' => [4, 5, 6]], $request->collect()->all());
}

public function testArrayAccess()
Expand Down