问题
I want to make drop-down list filtering.
I have a web page, that shown some post with title and categories.
The page has a drop-down in nav.blade.php. I dynamically generate drop-down from categories table. But when I select an item of drop-down (for example a category name) I want page to show me the posts only of that category. Also I have created the Category and Posts model and set the relationships. I can see all posts on my main page but can't filter content with drop-down list.
What I am doing wrong? and how can I solve this issue?
My nav.blade:
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
aria-haspopup="true" aria-expanded="false">Dropdown
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li>@foreach($categories as $category)
<a href="{{URL::route('home',$category->id)}}">
<option value="{{$category->id}}">{{ $category->name }}</option>
</a>
@endforeach
</li>
</ul>
</li>
回答1:
This would get you started:
Assuming you have a route like:
Route::get('/{category_id}', ['as'=>'home', 'uses'=>'PostController@show']);
In the PostController@show method:
public function show($category_id)
{
$categories = Category::all();
$selected_category = Category::with('posts')->where('id', $category_id)->first();
$posts = $selected_category->posts;
return redirect()->back()->with(compact('posts', 'categories'));
}
You can change the redirect location.
来源:https://stackoverflow.com/questions/36204060/laravel-5-2-filter-with-dropdownlist