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

darrylmorley's avatar

Use slug as query for API route

Hi, I'm trying to use a slug as a query for an API route, it's not working and returning a 404, but I'm unsure what I'm doing wrong.

Route::get('categories/{category:slug}', [CategoryController::class, 'show']);

public function show(Category $category)
    {
        return Category::find($category);
    }
0 likes
5 replies
Sinnbeck's avatar

Well first of all you cannot find a category by a category. You already have a category

public function show(Category $category)
    {
        return $category;
    }

Is the route in api.php or web.php? What url are you calling?

Sinnbeck's avatar

@darrylmorley can you show the full url you are calling? I assume the same happens if you open it in a browser?

Please or to participate in this conversation.