Home > Software design >  How to pass two functions in the same route in Laravel 8?
How to pass two functions in the same route in Laravel 8?

Time:12-01

i have this route:

web.php:

Route::get('main',[HomePageController::class,'show']);

that pass this function:

HomePageController.php

class HomePageController extends Controller
{
    function show()
    {
        $data = Classes::all();
        return view('index',['classes'=>$data]);
    }
}

and i want to pass another function in the same route but i keep getting "Undefined variable" whenever i try to do this:

web.php

Route::get('main',[HomePageController::class,'show']);
Route::get('main',[HomePageController::class,'showfeeds']);

HomePageController.php

class HomePageController extends Controller
{
    function show()
    {
        $data = Classes::all();
        return view('index',['classes'=>$data]);
    }

    function showfeeds()
    {
        $data = Feeds::all();
        return view('index',['feeds'=>$data]);
    }
}

what am i doing wrong here?

CodePudding user response:

You could use single route but send Classes and Feeds to single view (as you intended):

Route:

Route::get('main',[HomePageController::class,'show']);

Controller:

class HomePageController extends Controller
{
    function show()
    {
        $classes = Classes::all();
        $feeds = Feeds::all();
        return view('index', compact('classes', 'feeds'));
    }
}
  • Related