Search code examples
laravelapilaravel-8laravel-routing

How to set custom route name before main route in Laravel?


I want to add username before each route..

ex:

sam/productDashboard

james/productDashboard

note - Username is getting from session.

i tried like this. it doesn't work

Route::get( session()->get('name').'/productDashboard',[ProductController::class,'ProductDashboard'])->name('productDashboard');


Solution

  • This is not the way to use variable inside a route. Do it like this:

    Route::get('{username}/productDashboard',[ProductController::class,'ProductDashboard'])->name('productDashboard');
    

    and when you are referencing to this route with a link do it this way:

    <a href="{{route('productDashboard',['username' => session()->get('name')])}}">Link</>