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

mFeroNetwork's avatar

How to Fix URL Errors in Laravel 10?

Please help anyone for Laracasts members.

I am a new user and just learning laravel....

Okay, let's get straight to the point of the problem I am facing regarding laravel.

I am using laravel 10. Previously my site was normal and there were no problems.

But after a few days, suddenly the category URL changed, not as usual. The error URL shows as follows: example.com/products/category/1 or example.com/products/category/2

While the correct URL on my site is: example.com/products/travels or example.com/products/weddings

That's the error on my site.

How do I fix it? Thanks to anyone who answers and hopefully someone can help solve this problem.

Thank you.

0 likes
7 replies
Glukinho's avatar

Where exactly do you see those invalid URLs? If they are generated in a view, you should show appropriate code from it.

1 like
mFeroNetwork's avatar

@jayandholariya in beowser.

Here are the codes on the route:

Route::get('/', [FrontController::class, 'homepage']); Route::get('/products', [FrontController::class, 'products']); Route::get('/products/category/{category}', [FrontController::class, 'productCategory'])->name('product.category'); Route::get('/product/{slug}', [FrontController::class, 'productDetail'])->name('product.show'); Route::get('/posts', [FrontController::class, 'postIndex']); Route::get('/post/{slug}', [FrontController::class, 'postDetail'])->name('post.show'); Route::get('/p/invoice/{id}', [FrontController::class, 'showInvoice'])->name('invoice'); Route::get('/sitemap.xml', [FrontController::class, 'sitemap']); Route::get('/clear-cache', [FrontController::class, 'clearCache']); Route::get('/force-update', [UpdateController::class, 'forceUpdate']); Route::get('/{any}', [FrontController::class, 'any'])->where('any','^(?!api).*$');

And what are the solution steps to fix it?

jayandholariya's avatar

@mFeroNetwork In your route:

Route::get('/products/category/{category}', [FrontController::class, 'productCategory'])->name('product.category');

Laravel is automatically using route model binding, and by default, it uses the category’s ID.

If you want to show the slug (like travels or weddings) in the URL instead of the ID, you should do two things:

  1. In your Category model, add:
public function getRouteKeyName()
{
    return 'slug';
}

This tells Laravel to look up the category using the slug instead of the ID.

  1. When generating the link, pass the slug like this:
route('product.category', $category->slug)

Now the URL will be: /products/category/travels instead of /products/category/1

drgreen's avatar

You mean you want to resolve the URL using the category slug (like travels or weddings) instead of the ID (like 1 or 2)? Refer here for more on Customizing the key.

Check if you're defining you route as below:

Route::get('/products/category/{category:slug}', function (Category $category) {
    return $category;
});

Or, make sure you have this method defined in your Category model:

public function getRouteKeyName(): string
{
    return 'slug';
}

This tells Laravel to use the slug field instead of the id when resolving the route.

2 likes
mFeroNetwork's avatar

@drgreen Here are the codes on the route:

Route::get('/', [FrontController::class, 'homepage']); Route::get('/products', [FrontController::class, 'products']); Route::get('/products/category/{category}', [FrontController::class, 'productCategory'])->name('product.category'); Route::get('/product/{slug}', [FrontController::class, 'productDetail'])->name('product.show'); Route::get('/posts', [FrontController::class, 'postIndex']); Route::get('/post/{slug}', [FrontController::class, 'postDetail'])->name('post.show'); Route::get('/p/invoice/{id}', [FrontController::class, 'showInvoice'])->name('invoice'); Route::get('/sitemap.xml', [FrontController::class, 'sitemap']); Route::get('/clear-cache', [FrontController::class, 'clearCache']); Route::get('/force-update', [UpdateController::class, 'forceUpdate']); Route::get('/{any}', [FrontController::class, 'any'])->where('any','^(?!api).*$');

And what are the solution steps to fix it?

Snapey's avatar

so you are saying that the URLS that you generate within your app, lead to the wrong places?

In which case we need to see the code creating the link in the view.

Please or to participate in this conversation.