Skip to content

Commit ee08e77

Browse files
committed
Add range querying support
1 parent 379e1ac commit ee08e77

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/Repositories/AbstractRepository.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ abstract class AbstractRepository implements RepositoryContract
8787
*/
8888
protected $skipOrderingOnce = false;
8989

90+
/**
91+
* A set of keys used to perform range queries.
92+
*
93+
* @var array
94+
*/
95+
protected $range_keys = [
96+
'lt', 'gt',
97+
'bt', 'ne',
98+
];
99+
90100
/**
91101
* Create a new Repository instance
92102
*
@@ -376,6 +386,37 @@ public function search($queries)
376386
}
377387
}
378388

389+
// Get the range type
390+
$range_type = strtolower(substr($value, 0, 2));
391+
392+
// Perform a range based query if the range is valid
393+
// and the separator matches.
394+
if (substr($value, 2, 1) === ':' && in_array($range_type, $this->range_keys)) {
395+
// Get the true value
396+
$value = substr($value, 3);
397+
398+
switch ($range_type) {
399+
case 'gt':
400+
$query->where($this->appendTableName($columns[0]), '>', $value, 'and');
401+
break;
402+
case 'lt':
403+
$query->where($this->appendTableName($columns[0]), '<', $value, 'and');
404+
break;
405+
case 'ne':
406+
$query->where($this->appendTableName($columns[0]), '<>', $value, 'and');
407+
break;
408+
case 'bt':
409+
// Because this can only have two values
410+
if (count($values = explode(',', $value)) === 2) {
411+
$query->whereBetween($this->appendTableName($columns[0]), $values);
412+
}
413+
break;
414+
}
415+
416+
continue;
417+
}
418+
419+
// Create standard query
379420
if (count($columns) > 1) {
380421
$query->where(function ($q) use ($columns, $param, $value) {
381422
foreach ($columns as $column) {

0 commit comments

Comments
 (0)