0

I have a pagination-instance where I want to append all query parameter from the request to the next_page_url attribute.

I have query parameter with a value like &name=chris but I also have single parameter without a value like &xyz.

However, when I append all query parameters to the pagination instance, like so:

$query->simplePaginate(50)->appends($request->all());

only parameters with a value are getting appended.

How can I append all parameters to the next_page_url?

Update

I want to append query parameters to get the next chunk of requested data.

If I don't, it always gives back "next_page_url":"http://vue.dev/contacts?page=2". What I want is "next_page_url":"http://vue.dev/contacts?name&page=2"

13
  • Was wondering, why do you want to append a query parameter? Commented Jun 24, 2017 at 10:14
  • @joshuamabina to get the next chunk of requested data. If I don't append it always gives back "next_page_url":"http:\/\/vue.dev\/contacts?page=2". What I want is "next_page_url":"http:\/\/vue.dev\/contacts?name&page=2" Commented Jun 24, 2017 at 10:19
  • With what use do you have with the extra arguments (i.e. name)? The next_page_url is just supposed to hold the url of the next page. Commented Jun 24, 2017 at 10:25
  • Try this : $query->simplePaginate(50)->appends(Input::query()); Commented Jun 24, 2017 at 10:27
  • @SKJajoriya same result. Commented Jun 24, 2017 at 10:48

3 Answers 3

1

Take URL http://vue.dev/contacts?page=2&name for example. Although perfectly valid, it's still quite ambiguous. Do we mean to include name? Do we mean to exclude name?

So I'd suggest you to use this URL instead http://vue.dev/contacts?page=2&select=name. If you decide to select more stuff you can just do http://vue.dev/contacts?page=2&select=name,age,gender.

Later in your code just use explode to use the value as an array:

$attributes = explode(',', $request->select);

Useful reading: http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api

Sign up to request clarification or add additional context in comments.

Comments

0

Even though Fahmis solution is possible as well, I end up using the approach from this so-question. This has the advantage that php reads the parameter as an array automatically. My url end up looking like this:

http://vue.dev/contacts?page=2&select[]=xyz&select[]=abc

Comments

0

Use in service provider

Paginator::queryStringResolver(function () {
    $query = $this->app['request']->query();
    array_walk_recursive($query, function (&$value) {
        if ($value === null) {
            $value = '';
        }
    });
    return $query;
});

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.