Laravel 5.2 使用下拉列表进行过滤

Laravel 5.2 filter with dropdownlist

我想进行 drop-down 列表过滤。

我有一个网页,显示了一些 post 标题和类别。

该页面在 nav.blade.php 中有一个 drop-down。我从类别 table 动态生成 drop-down。但是当我 select drop-down 的项目(例如类别名称)时,我希望页面只显示该类别的 post。我还创建了类别和帖子模型并设置了关系。我可以在主页上看到所有 post,但无法使用 drop-down 列表过滤内容。

我做错了什么?我该如何解决这个问题?

我的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>

这会让你开始:

假设您的路线如下:

Route::get('/{category_id}', ['as'=>'home', 'uses'=>'PostController@show']);

PostController@show方法中:

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'));
}

您可以更改重定向位置。