I have a dropdown that I´m using to filter images by category. My first problem is that I want the selected choice to be selected after the filter, how can I do that?
This is my first time using Laravel and I wonder if I´m going in the right direction with my solution (now I have the same code in two functions, I´m planning on fixing that), but I can´t really figure out the best way of doing this. Can I have a function that takes either a category or null?
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" value="">Show all</button>
<ul class="dropdown-menu" role="menu">
@foreach ($categories as $category)
<li value="{{ $category->id }}"><a href="{{$category->id}}">{{ $category->name }}</a></li>
@endforeach
</ul>
</div>
routes
Route::get('/', array('uses' => 'MyController@index'));
Route::get('/{category?}', array('uses' => 'MyController@filter'));
controller
public function index()
{
$images = Image::all();
$categories = ..\Models\Category::all();
return View::make('index', array('images' => $images, 'categories' => $categories));
}
public function filter($category){
$images = Image::where('category_id', '=', $category);
$categories = ..\Models\Category::all();
return View::make('index', array('images' => $images, 'categories' => $categories));
}