Laravel 5.2过滤器和下拉列表 [英] Laravel 5.2 filter with dropdownlist

查看:116
本文介绍了Laravel 5.2过滤器和下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



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



我做错了什么?我怎样才能解决这个问题?



我的nav.blade:

 < li class =dropdown> 
< a href =#class =dropdown-toggledata-toggle =dropdownrole =button
aria-haspopup =truearia-expanded =false> ;下拉菜单
< 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'));
}

您可以更改重定向位置。

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>

解决方案

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.

这篇关于Laravel 5.2过滤器和下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆