Laravel 5.2 filter with dropdownlist

亡梦爱人 提交于 2020-01-11 11:51:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!