8
votes

Route & Prefix has same name. I'm not able to get ID parameter of {hotel} which is empty as i mention below in image. What is the best way to use prefix and resource controller with same name?

Routes/web.php

Route::namespace('Admin\Hotel')->prefix('hotels')->name('hotels.')->group(function () {
    Route::resource('/', 'HotelController');
    Route::resource('rooms', 'RoomController');
    Route::resource('rooms/gallery', 'RoomGalleryController');
});

php artisan route:list for Route::resource('/', 'HotelController')

enter image description here

4
I don’t understand your question. Can you be a bit clearer, please?Martin Bean
same the question is not clearSagar Ahuja
URI: http:example.com/admin/hotels route group prefix name is hotels and my resource controller name is hotels as well. If prefix & resource controller name is same then what is the best way to use it?Fay
@MartinBean i have updated the question.Fay

4 Answers

7
votes

it's because resource method will automaticly add the prefix and the named routes with the first parameter you give, hotel in your case.

So you can do something like this :

Route::namespace('Admin\Hotel')->group(function () {
    Route::resource('hotels', 'HotelController');
});

Or, you can remove group function and directly use resource method.

Route::resource('hotels', 'Admin\Hotel\HotelController');

Or,

Route::namespace('Admin\Hotel')->group(function () {
    Route::resource('hotels', 'HotelController');
    Route::prefix('hotels')->name('hotels.')->group(function () {
        Route::resource('gallery', 'HotelGalleryController');
        Route::resource('rooms', 'RoomController');
        Route::resource('rooms/gallery', 'RoomGalleryController');
    });
});
3
votes

Sometimes you want to keep it grouped, so the solution is to use the parameters method.

Route::namespace('Admin\Hotel')->prefix('hotels')->name('hotels.')->group(function () {
Route::resource('/', 'HotelController')->parameters(['' => 'hotel']);
Route::resource('rooms', 'RoomController');
Route::resource('rooms/gallery', 'RoomGalleryController');
});
2
votes

I think using the group method would be better, try out this.

Route::group(['namespace' => 'Admin\Hotel', 'prefix' => 'hotel'], function(){
    ...
});
0
votes

I have the same scenario in mixing Group and Resource and I can't get the ID group (which in this case Hotel).

Here's how I did it in (Laravel 5.5):

Route::group(['prefix' => 'hotel/{hotel}'], function () {
    Route::resource('/', 'HotelController');
    Route::resource('rooms', 'RoomController');
    Route::resource('rooms/gallery', 'RoomGalleryController');
});