Daniel-Pablo's avatar

slug on a resource method HELP!! how to do it... :(

am having a hard issue... I create a resource... buy no idea how to set up a service slug check please

Route::resource('/user/{user}/services', 'ServiceController' , [
    'names' => [
        'index' => 'services',
        'create' => 'newService',
        'store' => 'storeService',
        'edit' => 'editService',
        'update' => 'updateService'
    ]
])

after doing this way I got to change the code to this new one... but I like the first one but don't have any idea how to do it right

check the code I am using...

Route::get('/user/{user:name}/services', 'ServiceController@index')->name('services');
Route::get('/user/{user:name}/services/create', 'ServiceController@create')->name('newService');
Route::post('/user/{user:name}/services', 'ServiceController@store')->name('storeService');
Route::get('/user/{user:name}/services/{service:slug}/edit', 'ServiceController@edit')->name('editService');
Route::post('/user/{user:name}/services/{service:slug}', 'ServiceController@update')->name('updateService');

first call the user name... then call the service by it slug, I want to do the same but in the resource method... som idea how to do it?... thanks

0 likes
3 replies
Sti3bas's avatar
Sti3bas
Best Answer
Level 53
Route::resource('user.services', 'ServiceController' , [
    'names' => [
        'index' => 'services',
        'create' => 'newService',
        'store' => 'storeService',
        'edit' => 'editService',
        'update' => 'updateService'
    ]
])

Add this to User model:

public function getRouteKeyName()
{
    return 'name';
}

Add this to Service model:

public function getRouteKeyName()
{
    return 'slug';
}

https://laravel.com/docs/7.x/controllers#restful-nested-resources

https://laravel.com/docs/7.x/routing#implicit-binding

Daniel-Pablo's avatar

:) it works!! THANKS!!! dont understand how!!! but I am digging it :) very grate full

Please or to participate in this conversation.