From 13381abfccb70c29af376b88773619892c421899 Mon Sep 17 00:00:00 2001 From: Abdullah Ghanem <3bdullah.ghanem@gmail.com> Date: Sat, 3 Dec 2022 03:53:22 +0200 Subject: [PATCH 1/2] Update Lists.php --- src/Traits/Lists.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/Traits/Lists.php b/src/Traits/Lists.php index ed8d27d..5717d48 100755 --- a/src/Traits/Lists.php +++ b/src/Traits/Lists.php @@ -6,6 +6,26 @@ trait Lists { + /** + * Fetch all time trending subjects Ids. + * + * @param int $limit + * @param array $constraints optional. filter models by attributes (where=[...]) + * @return array + */ + public function topIds($limit = 5, $orderByAsc = false, $constraints = []) + { + if(is_array($orderByAsc)) { + $constraints = $orderByAsc; + $orderByAsc = false; + } + + $cacheKey = $this->keys->cache($limit, $orderByAsc, $constraints); + + $cachedList = $this->cachedList($limit, $cacheKey); + return $this->getVisitsIds($limit, $this->keys->visits, $orderByAsc); + } + /** * Fetch all time trending subjects. * @@ -71,6 +91,19 @@ protected function getSortedList($name, $limit, $orderByAsc = false, $withValues return $this->connection->valueList($this->keys->visits . "_{$name}:{$this->keys->id}", $limit, $orderByAsc, $withValues); } + + /** + * Fetch lowest subjects Ids. + * + * @param int $limit + * @param array $constraints optional + * @return array + */ + public function lowIds($limit = 5, $constraints = []) + { + return $this->topIds($limit, true, $constraints); + } + /** * Fetch lowest subjects. * From 8f3d2843336a637d997d2260ee6762aebed48561 Mon Sep 17 00:00:00 2001 From: Abdullah Ghanem <3bdullah.ghanem@gmail.com> Date: Sat, 3 Dec 2022 03:54:55 +0200 Subject: [PATCH 2/2] Update 7_visits-lists.md --- docs/7_visits-lists.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/7_visits-lists.md b/docs/7_visits-lists.md index b1623b0..61be1cd 100644 --- a/docs/7_visits-lists.md +++ b/docs/7_visits-lists.md @@ -12,6 +12,20 @@ visits('App\Post')->top(10); visits('App\Post')->low(10); ``` +Top or Lowest list ids + +## Top/Lowest visited items Ids + +```php +visits('App\Post')->topIds(10); +``` + +```php +visits('App\Post')->lowIds(10); +``` + + + ### Filter by model attributes 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: