Search code examples
laravellaravel-bladelaravel-routing

Add locale to laravel resource route


I have following route that expect locale in prefix

Route::group(['prefix' => '{locale}/admin', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => 'setlocale'], function() {
    Route::resource('/profile', 'Admin\ProfileController', ['except' => ['edit', 'create', 'destroy', 'show', 'store']]);
});

The final route supposed to be like this:

www.example.com/en/admin/profile
and
www.example.com/en/admin/profile/1

while I can get index route www.example.com/en/admin/profile, but I cannot get update route www.example.com/en/admin/profile/1 instead I have this

www.example.com/1/admin/profile/en

Here is how my routes are defined in blade

// Return index route correctly
{{route('profile.index', app()->getLocale())}}

//And 

{{route('profile.update', [$user->id, app()->getLocale()])}}
// Return update route wrong as I mentioned above.

My question is: How should I define my update route to return correct url?


Solution

  • You are passing your parameters in wrong order. You need to send locale first and $user->id after that.

    {{ route('profile.update', [app()->getLocale(), $user->id]) }}