Skip to content

Commit 6017484

Browse files
authored
Merge pull request #86 from AbdullahGhanem/master
Add new Two Methods for TopIds and LowIds
2 parents 4e1b746 + 8f3d284 commit 6017484

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

docs/7_visits-lists.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ visits('App\Post')->top(10);
1212
visits('App\Post')->low(10);
1313
```
1414

15+
Top or Lowest list ids
16+
17+
## Top/Lowest visited items Ids
18+
19+
```php
20+
visits('App\Post')->topIds(10);
21+
```
22+
23+
```php
24+
visits('App\Post')->lowIds(10);
25+
```
26+
27+
28+
1529
### Filter by model attributes
1630

1731
You can get only some of the top/low models by query where clause. For example if Post model has `shares` & `likes` attributes you can filter the models like this:

src/Traits/Lists.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@
66

77
trait Lists
88
{
9+
/**
10+
* Fetch all time trending subjects Ids.
11+
*
12+
* @param int $limit
13+
* @param array $constraints optional. filter models by attributes (where=[...])
14+
* @return array
15+
*/
16+
public function topIds($limit = 5, $orderByAsc = false, $constraints = [])
17+
{
18+
if(is_array($orderByAsc)) {
19+
$constraints = $orderByAsc;
20+
$orderByAsc = false;
21+
}
22+
23+
$cacheKey = $this->keys->cache($limit, $orderByAsc, $constraints);
24+
25+
$cachedList = $this->cachedList($limit, $cacheKey);
26+
return $this->getVisitsIds($limit, $this->keys->visits, $orderByAsc);
27+
}
28+
929
/**
1030
* Fetch all time trending subjects.
1131
*
@@ -71,6 +91,19 @@ protected function getSortedList($name, $limit, $orderByAsc = false, $withValues
7191
return $this->connection->valueList($this->keys->visits . "_{$name}:{$this->keys->id}", $limit, $orderByAsc, $withValues);
7292
}
7393

94+
95+
/**
96+
* Fetch lowest subjects Ids.
97+
*
98+
* @param int $limit
99+
* @param array $constraints optional
100+
* @return array
101+
*/
102+
public function lowIds($limit = 5, $constraints = [])
103+
{
104+
return $this->topIds($limit, true, $constraints);
105+
}
106+
74107
/**
75108
* Fetch lowest subjects.
76109
*

0 commit comments

Comments
 (0)