Ad

How To Generate A Url With Laravel's Url Generator Helper With Parameters That Are Not At The End?

- 1 answer

Normally it is very simple to add parameters using the Laravel url helper method -- as simple as:

url("foo/show/", [333]); // site.com/foo/show/333

However, how do you do it when parameters are at a midpoint, such as:

url("foo/?/bar/?/show", [333,444]); 

I have tried the following which all fail (some come close though):

url("foo/?/bar/?/show", [333,444]); 
// site.com/foo//333/444?/bar/?/show

url("foo/{a}/bar/{b}/show", [333,444]);
// site.com/foo/{a}/bar/{b}/show/333/444

url("foo/{a}/bar/{b}/show", ['a' => 333, 'b' => 444]);
// site.com/foo/{a}/bar/{b}/show/333/444

The ? is obviously important, because it gets close to the result...

Note

I am specifically talking about unnamed routes. So the route function is not in question. Named routes aren't possible in the current project, so I need to reiterate I am looking for the use of the url method.

I 100% agree that normally named routes are the way to go.

Ad

Answer

Try this:

url(sprintf("foo/%d/bar/%d/show", 333, 444)); 
Ad
source: stackoverflow.com
Ad