Laravel named route URL with parameter not working as expected

I'm trying to generate a url with parameter using laravel route helper,

route('frontend.admin.categories.edit', $catRequest->id)

but this is the generated url

http://localhost:8000/admin/categories/edit?1

I need a URL like this

http://localhost:8000/admin/categories/edit/1

And this is my route

 Route::get('admin/categories/edit/{id}', 'CategoryController@edit')
        ->name('admin.categories.edit');

What is the issue here?

Upvotes: 0

Views: 1101

Answers (4)

You may have another route with the same name right after this one in the code that may be overwriting the above route params, i had the same problem and in my case this is what was happening, i had two routes definitions using the same name but with different http methods like the example below. Ex:

Route::get('/route1/{param1}');
Route::post('/route1');
redirect()->route('/route1', ['test']); // output: /route1?test

When i've changed it to:

Route::post('/route1');
Route::get('/route1/{param1}');
redirect()->route('/route1', ['test']); // output: /route1/test

then i got the desired result.

Upvotes: 0

Maulik Shah
Maulik Shah

Reputation: 1050

Try to view your route list using php artisan route:list. If you found your route in list with correct Uri then see that you are not again specify that route with same Uri or same alias name in your route file.

Hope this helps :)

Upvotes: 0

Script47
Script47

Reputation: 14550

You can specify the parameter(s) you are replacing by passing an associative array instead of a variable.

Change,

route('frontend.admin.categories.edit', $catRequest->id)

To

route('frontend.admin.categories.edit', [
    'id' => $catRequest->id
]);

Edit #1

You are calling the wrong route, you named your route as admin.categories.edit in the definition yet you are calling frontend.admin.categories.edit within the helper function which is not the originally defined route. So your code should be:

route('admin.categories.edit', [
    'id' => $catRequest->id
]);

Reading Material

Named Routes

Upvotes: 1

Vincent Decaux
Vincent Decaux

Reputation: 10714

You need to use an array for your params :

route('frontend.admin.categories.edit', ['id' => $catRequest->id])

If not, all params will be used as $_GET params.

Upvotes: 0

Related Questions