Fay
Fay

Reputation: 301

Route prefix with resource controller - Laravel 5.5

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

Upvotes: 15

Views: 20404

Answers (4)

Player1
Player1

Reputation: 3166

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');
});

Upvotes: 2

Arnas Kazlauskas
Arnas Kazlauskas

Reputation: 479

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');
});

Upvotes: 12

collins
collins

Reputation: 86

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

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

Upvotes: 2

Julien METRAL
Julien METRAL

Reputation: 1974

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');
    });
});

Upvotes: 14

Related Questions