Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mediax's avatar

Route URL with 2 parameters? Get only 404 page

I work with laravel 5.3 and want to have slug and id in my url, like "/my-product-357/"... Only with slug it works, but if I add id then I get only 404!

My Route is: Route::get('/{slug}-{id}', ['uses' => 'ProductController@show'])->name('product.show');

My link to the resource is generated like: {{ route('product.show', ['slug' => $product->slug, 'id' => $product->id]) }}

This is ok. the url what I want is there like /my-product-343...

My Controller is: public function show($slug, $id) { $product = Product::SlugAndId($slug, $id)->firstOrFail(); return view('product.show', compact('product')); }

What I do wrong here?

0 likes
8 replies
bestmomo's avatar

Hello

With '/{slug}-{id}' when you have this url : /my-product-343, slug = my and id = product and you have additionnal information so your route is wrong.

2 likes
mediax's avatar

Testes, it's true, many thanks. How can I solve this problem? Like escape...?

mediax's avatar

Ok. it solved my problem, is possible also to use "-"?

bestmomo's avatar

You must choose another separator to avoid ambiguity.

mediax's avatar

Done, many thanks for your help!

Ori's avatar
Ori
Best Answer
Level 4

You do not have to change your separator. Use regex instead

Route::get('/{slug}-{id}', function ($slug, $id) {
    return [$slug, $id];
})->where('slug', '([0-9A-Za-z\-]+)')->where('id', '([0-9]+)');

Hope it helps

1 like

Please or to participate in this conversation.