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"

Paulie-C
  • 1,674
  • 1
  • 13
  • 29
F.M.F.
  • 1,929
  • 3
  • 23
  • 42
  • Was wondering, why do you want to append a query parameter? –  Jun 24 '17 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" – F.M.F. Jun 24 '17 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**. –  Jun 24 '17 at 10:25
  • Try this : `$query->simplePaginate(50)->appends(Input::query());` – Sagar Jajoriya Jun 24 '17 at 10:27
  • @SKJajoriya same result. – F.M.F. Jun 24 '17 at 10:48
  • @joshuamabina the parameter without value are the columns that get selected from the query – F.M.F. Jun 24 '17 at 10:49
  • You can make it `&select=xyz` if needed since the `appends` always adds it as a key value pair. – ayip Jun 24 '17 at 12:22
  • As @joshuamabina have said, why do you need `name` in the URL? Will that actually toggle something on/off? – Fahmi Jun 24 '17 at 12:42
  • @Fahmi yes it will change the 'appearance' of the data – F.M.F. Jun 24 '17 at 16:07
  • @ayip I could do that, just thought there would be a way without the overhead... – F.M.F. Jun 24 '17 at 16:08
  • Alright then just do as @ayip suggested. `&name` alone doesn't quite "follow" REST convention since it doesn't fully describe what you mean by that. By doing for example `&select=name,age,gender`, it clearly shows its purpose. – Fahmi Jun 24 '17 at 16:21
  • @Fahmi did it that way with `&select[]=xyz&select[]=abc`. Just answer the question that I can accept for future reference. – F.M.F. Jun 24 '17 at 16:38
  • Added my answer. I wouldn't suggest you to use `&select[]=xyz&select[]=abc` since we can achieve the same result with a cleaner looking URL. Have a look at my answer to see what I mean. – Fahmi Jun 25 '17 at 08:35

2 Answers2

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

Fahmi
  • 2,607
  • 1
  • 20
  • 15
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
F.M.F.
  • 1,929
  • 3
  • 23
  • 42