F.M.F.
F.M.F.

Reputation: 2292

Laravel pagination: Append query parameter without value

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"

Upvotes: 0

Views: 2551

Answers (2)

F.M.F.
F.M.F.

Reputation: 2292

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

Upvotes: 0

Fahmi
Fahmi

Reputation: 2673

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

Upvotes: 1

Related Questions