Skip to content

Commit

Permalink
#12 - add dynamic appends support
Browse files Browse the repository at this point in the history
  • Loading branch information
selahattinunlu committed Nov 27, 2016
1 parent 73ffd51 commit 3a0f429
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
38 changes: 36 additions & 2 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class QueryBuilder

protected $excludedParameters = [];

protected $appends = [];

protected $query;

protected $result;
Expand Down Expand Up @@ -88,7 +90,13 @@ public function build()

public function get()
{
return $this->query->get();
$result = $this->query->get();

if ($this->hasAppends()) {
$result = $this->addAppendsToModel($result);
}

return $result;
}

public function paginate()
Expand All @@ -97,7 +105,13 @@ public function paginate()
throw new Exception("You can't use unlimited option for pagination", 1);
}

return $this->basePaginate($this->limit);
$result = $this->basePaginate($this->limit);

if ($this->hasAppends()) {
$result = $this->addAppendsToModel($result);
}

return $result;
}

public function lists($value, $key)
Expand Down Expand Up @@ -234,6 +248,11 @@ private function setWheres($parameters)
$this->wheres = $parameters;
}

private function setAppends($appends)
{
$this->appends = explode(',', $appends);
}

private function addWhereToQuery($where)
{
extract($where);
Expand Down Expand Up @@ -310,6 +329,11 @@ private function hasIncludes()
return (count($this->includes) > 0);
}

private function hasAppends()
{
return (count($this->appends) > 0);
}

private function hasGroupBy()
{
return (count($this->groupBy) > 0);
Expand Down Expand Up @@ -352,6 +376,16 @@ private function customFilterName($key)
return 'filterBy' . studly_case($key);
}

private function addAppendsToModel($result)
{
$result->map(function($item) {
$item->append($this->appends);
return $item;
});

return $result;
}

/**
* Paginate the given query.
*
Expand Down
3 changes: 2 additions & 1 deletion src/UriParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class UriParser
'limit',
'page',
'columns',
'includes'
'includes',
'appends'
];

protected $uri;
Expand Down

0 comments on commit 3a0f429

Please sign in to comment.