Skip to content

Commit

Permalink
Merge pull request #86 from AbdullahGhanem/master
Browse files Browse the repository at this point in the history
Add new Two Methods for TopIds and LowIds
  • Loading branch information
BSN4 authored Dec 3, 2022
2 parents 4e1b746 + 8f3d284 commit 6017484
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/7_visits-lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 33 additions & 0 deletions src/Traits/Lists.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 6017484

Please sign in to comment.