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

Codes09's avatar

Route params inverted in controller

Seeing some weirdness in a route I'm building.

Route::get( '/quiz/{quiz_name}/{index}', [ \App\Http\Controllers\QuizController::class, 'show'])

When the controller show method is called $index and $quiz_name are inverted

    public function show($index, string $quiz_name )
    {
        dd('index = ' . $index, 'quiz = ' . $quiz_name);
    }

dd output

"index = quiz" // app/Http/Controllers/QuizController.php:47
"quiz = 1" // app/Http/Controllers/QuizController.php:47

I've tried swapping the order of the show() params but this does not change anything unfortunately.

Why would $index and $quiz_name be inverted in this case?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It seems like there might be a mismatch between the route parameters and the method parameters in your controller. The parameters are passed to the controller method in the order they are defined in the route. To fix the issue, you should ensure that the order of parameters in your controller's method signature matches the order of the route parameters.

Here's how you can correct the show method in your QuizController:

public function show($quiz_name, $index)
{
    dd('quiz_name = ' . $quiz_name, 'index = ' . $index);
}

Make sure that the order of parameters in the method show($quiz_name, $index) matches the order in the route definition Route::get('/quiz/{quiz_name}/{index}', ...). This way, Laravel will inject the correct values into the correct parameters when the route is called.

If you still face issues, you might want to clear your route cache (if you're caching your routes) by running the following Artisan command:

php artisan route:clear

This will clear the route cache and ensure that any changes you've made to your routes are being recognized by the application.

Please or to participate in this conversation.