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

[5.7] Collection whereBetween #26888

Merged
merged 3 commits into from
Dec 18, 2018
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
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,18 @@ public function whereInStrict($key, $values)
return $this->whereIn($key, $values, true);
}

/**
* Filter items where the given key between values.
*
* @param string $key
* @param array $values
* @return static
*/
public function whereBetween($key, $values)
{
return $this->where($key, '>=', reset($values))->where($key, '<=', end($values));
}

/**
* Filter items by the given key value pair.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,16 @@ public function testValues()
})->values()->all());
}

public function testBetween()
{
$c = new Collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);

$this->assertEquals([['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]],
$c->whereBetween('v', [2, 4])->values()->all());
$this->assertEquals([['v' => 1]], $c->whereBetween('v', [-1, 1])->all());
$this->assertEquals([['v' => 3], ['v' => '3']], $c->whereBetween('v', [3, 3])->values()->all());
}

public function testFlatten()
{
// Flat arrays are unaffected
Expand Down