Skip to content

Commit 3a0f429

Browse files
#12 - add dynamic appends support
1 parent 73ffd51 commit 3a0f429

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/QueryBuilder.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class QueryBuilder
3838

3939
protected $excludedParameters = [];
4040

41+
protected $appends = [];
42+
4143
protected $query;
4244

4345
protected $result;
@@ -88,7 +90,13 @@ public function build()
8890

8991
public function get()
9092
{
91-
return $this->query->get();
93+
$result = $this->query->get();
94+
95+
if ($this->hasAppends()) {
96+
$result = $this->addAppendsToModel($result);
97+
}
98+
99+
return $result;
92100
}
93101

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

100-
return $this->basePaginate($this->limit);
108+
$result = $this->basePaginate($this->limit);
109+
110+
if ($this->hasAppends()) {
111+
$result = $this->addAppendsToModel($result);
112+
}
113+
114+
return $result;
101115
}
102116

103117
public function lists($value, $key)
@@ -234,6 +248,11 @@ private function setWheres($parameters)
234248
$this->wheres = $parameters;
235249
}
236250

251+
private function setAppends($appends)
252+
{
253+
$this->appends = explode(',', $appends);
254+
}
255+
237256
private function addWhereToQuery($where)
238257
{
239258
extract($where);
@@ -310,6 +329,11 @@ private function hasIncludes()
310329
return (count($this->includes) > 0);
311330
}
312331

332+
private function hasAppends()
333+
{
334+
return (count($this->appends) > 0);
335+
}
336+
313337
private function hasGroupBy()
314338
{
315339
return (count($this->groupBy) > 0);
@@ -352,6 +376,16 @@ private function customFilterName($key)
352376
return 'filterBy' . studly_case($key);
353377
}
354378

379+
private function addAppendsToModel($result)
380+
{
381+
$result->map(function($item) {
382+
$item->append($this->appends);
383+
return $item;
384+
});
385+
386+
return $result;
387+
}
388+
355389
/**
356390
* Paginate the given query.
357391
*

src/UriParser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class UriParser
1818
'limit',
1919
'page',
2020
'columns',
21-
'includes'
21+
'includes',
22+
'appends'
2223
];
2324

2425
protected $uri;

0 commit comments

Comments
 (0)