Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 615
Fay Avatar asked Jul 10 '18 12:07

Fay


2 Answers

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');
});
like image 97
Player1 Avatar answered Oct 04 '22 04:10

Player1


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');
    });
});
like image 42
Julien METRAL Avatar answered Oct 04 '22 04:10

Julien METRAL