tomasosho's avatar

how to use slug instead of id

i have my route as

Route::resource('profile/{slug}', 'ProfileController');

My Index.php

public function index(User $slug)
    {

    }
0 likes
3 replies
MichalOravec's avatar
Level 75

Add this to your User model

/**
 * Get the route key for the model.
 *
 * @return string
 */
public function getRouteKeyName()
{
    return 'slug';
}

Then just use

Route::resource('profile', 'ProfileController');

But if you want just one route then

Route::get('profile/{user:slug}', 'ProfileController@show');
public function show(User $user)
{
    //
}
2 likes

Please or to participate in this conversation.