-1

Is it bad to put parameter in the route group prefix because in certain pages i get non object error. For most pages it works but it seems it is not working for Route::get('/{categories}', 'AdminVisible\CostIncludeController@index');

Auth::routes(['register' => false,'login' => false]);
Route::prefix('admin')->group(function() {
    Route::get('/')->name('login')->uses('Auth\LoginController@showLoginForm');
    Route::post('/')->name('login')->uses('Auth\LoginController@login');
    Route::get('/dashboard', 'AdminVisible\HomeController@index')->name('admin.dashboard');
    Route::prefix('pages')->group(function() {
        Route::get('/','AdminVisible\AdminPageController@pages')->name('pages');
        Route::prefix('{page}')->group(function() {
            Route::get('/','AdminVisible\AdminPageController@index')->name('page');
            Route::get('/banner', 'AdminVisible\BannerController@index');
            Route::get('/why-with-us', 'AdminVisible\WhyWithUsController@index');
            Route::get('/testimonials', 'AdminVisible\TestimonialsController@index');
            Route::get('/about', 'AdminVisible\AboutController@index');
            Route::get('/about-why-with-us', 'AdminVisible\AboutWhyWithUsController@index');
            Route::get('/general-information', 'AdminVisible\PackageController@index');
            Route::get('/package-program', 'AdminVisible\PackageController@index');
            Route::prefix('cost-include')->group(function() {
                Route::get('/', 'AdminVisible\PackageController@index');
                Route::get('/{categories}', 'AdminVisible\CostIncludeController@index');
            });
        });
    }); 
});

My AdminPageController:

    public function index($page)
    {
        $page = Page::where('Pages_Slug_Name',$page)->firstorFail();
        $pages = Page::all();
        return view('admin.pages.page',[
            'page' => $page,
        ],compact('pages'));
    }

My CostIncludeController:

    public function index($categories){
        $pages = Page::all();
        $packages = Package::where('slug',$categories)->first();
        return view('admin.pages.costinclude',[
            'packages' => $packages,
        ],compact('pages'));    
    }

With {page} prefix: I thik this error means the page does not exist but it exist. enter image description here If the {page} prefix if removed: enter image description here

Nutan Panta
  • 59
  • 1
  • 16

1 Answers1

1

Both the page and the categories parameters are passed to your functions, but your index in CostIncludeController only has $categories, so

public function index($categories){

should be

public function index($page, $categories){

As pages is the first parameter to be passed, you must make sure it also the first argument. index($categories, $page) would also be wrong.

MrEvers
  • 1,040
  • 1
  • 10
  • 18
  • thaks i been trying to fix this from yesterday and i am new to php and laravel so i did not know that we need to to pass it like that in the parameter. – Nutan Panta Aug 14 '20 at 12:03
  • 1
    Yes, the parameters in your routes and the variables in your function don't need to have the same name, they are passed and filled in one by one in order. so when you did index($categories), the $categories variable actually had the value from the 'page' parameter – MrEvers Aug 14 '20 at 12:05
  • thanks i learned something new. – Nutan Panta Aug 14 '20 at 12:06