Tuesday, April 11, 2017

route with parameters for search results laravel

I have a simple event list, which I take from the Events table. The view where I list the events has a sidebar with four categories. I want, when clicking on a category, to access the route "events?category=2" for example and show only the events in that category.

I created a service provider which provides the categories from the database table "categories" for the table, and the link in the sidebar is

<a href="">

The routes are

Route::get('events', 'EventsController@mainList')->name('events');
Route::get('searchEventByCategory','EventsController@searchEventByCategory')->name('searchByCategory');

And the EventsController is as follows:

public function mainList(Request $request){
    $events = Event::all();
    return view('pages.events', compact('events'));
}

public function searchEventByCategory(Request $request){
    $events = Event::where('category',$request->query('id'));
    return redirect()->route('events', compact('events'));
}

This doesn't work, as, on calling the controller in the 'events' route, the $events variable is overridden, even if I pass it as argument to the route.

I can't think of anything to go around this problem. How could I approach it?



via Ileana Profeanu

Advertisement