-
Notifications
You must be signed in to change notification settings - Fork 65
Closed
Description
Improving: add original query string on paginate() method
Problem
The current paginate() method does not includes the original query parameters on "url pages"
http://localhost:8025/app/public/users?includes=city&country=US&limit=10&page=4
results ...
[...]
"next_page_url": "http://localhost:8025/app/public/users?page=4",
[...]
Proposal
//QueryBuilder.php
[...]
$qs = [];
parse_str($this->uriParser->hasQueryUri(), $qs);
$resultado = $this->query->paginate($this->limit)->appends($qs);
[...]
Now "next_page_url" and "prev_page_url" includes the original query string
{
"next_page_url": "http://localhost:8025/app/public/users?includes=city&country=US&limit=10&page=4",
"prev_page_url": "http://localhost:8025/app/public/users?includes=city&country=US&limit=10&page=4",
Supporting dynamic appends() on Laravel models
Static way
On Laravel models we have "$appends = ['is_admin', 'balance']" attribute that allow us to add some custom attributes to our model.
Dynamic way
$user = User::find(1)->append(['is_admin', 'balance']);
public function getIsAdminAttribute()
{
$this->attributes['is_admin'] = 'yes'; // some logic in here
}
public function getBalanceAttribute()
{
$this->attributes['balance'] = '1200.00'; // some logic in here
}
Full solution : fixing query string on paginate() and adding support to dynamic append()
http://localhost:8025/app/public/users?&appends=is_admin,balance
//QueryBuilder.php
public function paginate()
{
if (! $this->hasLimit()) {
throw new Exception("You can't use unlimited option for pagination", 1);
}
//******* EDIT
$qs = [];
parse_str($this->uriParser->hasQueryUri(), $qs);
$result = $this->query->paginate($this->limit)->appends($qs);
if ($this->uriParser->hasQueryParameter('appends') <> NULL)
{
$result->map(function($item) {
return $item->append(explode(',', $this->uriParser->queryParameter('appends')['value']));
});
}
//******* END
return $result;
}
NOTE: The same append() logic would be applied on get() method (except "query string" issue).
Metadata
Metadata
Assignees
Labels
No labels