Bogdan
Bogdan

Reputation: 165

Laravel multiple named optional parameters where order don't matter

I'm trying to achieve the following routing in Laravel

/stats/{id}
/stats/{id}/limit/{limit}
/stats/{id}/limit/{limit}/order/{order}
/stats/{id}/order/{order}
/stats/{id}/order/{order}/limit/{limit}

As you can see, only {id} is required, /limit/ and /order/ may or may not appear and their order shouldn't matter.

Thank you!

Upvotes: 1

Views: 168

Answers (2)

Milan Palangetic
Milan Palangetic

Reputation: 99

I'm not sure what you're trying to achieve but I think that the best approach to this is to send limit and order as query params instead of creating route options for them. Like:

/stats/{id}?order={order}&limit={limit}

This way you can add as many optional params as you want.

Upvotes: 2

Sand Of Vega
Sand Of Vega

Reputation: 2416

Use ? a question mark after parameter. Like:

/stats/{id}/order/{order?}/limit/{limit?}

Source: Laravel Route Optional Parameters

Upvotes: 0

Related Questions