Laravel 5:如何按特定类别获取帖子并显示在选项卡中

时间:2018-07-13 10:10:08

标签: laravel laravel-5 laravel-blade

我正在建立一个新闻站点。我想按3个特定类别获取6个最新帖子,比如说id 2、3、4,并在首页的3个标签中显示它们。
每个选项卡分为2列,列6。一个col-6显示最新记录,另一个显示最新记录后的另外5条记录的列表。

这是我的控制器,我从数据库中获得6条最新记录

  

$ post_tabs = Post :: with('categories')-> latest()-> limit(6)-> get();

我得到3个ID为2、3、4的类别

  

$ category_tabs = Category :: with('posts')-> whereIn('id',[2,3,4])-> get();

在刀片视图中,我设法显示了3个类别标签,就像这样

<div class="collapse navbar-collapse justify-content-between" id="navbarToggler1">
<ul class="nav nav-sm navbar-nav mr-md-auto mr-lg-0 mt-2 mt-lg-0 align-self-end" role="tablist">
    @foreach($category_tabs as $tab_category)
        <li class="nav-item">
            <a class="nav-link bg-color-tech active" id="nav-outdoor-tab-{{$tab_category->id}}" data-toggle="tab" href="#nav-outdoor-{{$tab_category->id}}" role="tab" aria-selected="true">
                {{$tab_category->title}}
            </a>
        </li>
    @endforeach
</ul></div>

现在,我不知道如何通过标签显示这些类别的帖子,这是我到目前为止所获得的信息,仅显示6条最新帖子,我也不知道如何使标签起作用>

@foreach($post_tabs as $key => $post_tab)
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-outdoor-{{-- {{$tab_category->id}} --}}" role="tabpanel">
    <div class="row clearfix">
        <div class="col-lg-6">
            @if ($loop->first)
            <article>
                <div class="entry-image mb-3">
                    <a href="{{route('post.show',[$post_tab->slug])}}"><img src="{{asset($post_tab->thumbnail)}}" alt="{{$post_tab->title}}"></a>
                    @foreach($post_tab->categories as $category)
                    <div class="entry-categories"><a href="#" class="bg-tech">{{$category->title}}</a></div>
                    @endforeach
                </div>
                <div class="entry-title">
                    <h3><a target="_blank" href="{{route('post.show',[$post_tab->slug])}}">{{$post_tab->title}}</a></h3>
                </div>
                <div class="entry-content clearfix">
                    <p>{{$post_tab->description}}</p>
                </div>
            </article>
        </div>
        <div class="col-lg-6">
            @foreach($post_tabs as $key => $post_tab)
            @if($key > 0) 
            <article>
                <div class="entry-image">
                    <a href="#"><img src="{{asset($post_tab->thumbnail)}}" alt=""></a>
                </div>
                <div class="entry-c">
                    <div class="entry-title">
                        <h4><a href="#">{{$post_tab->title}}</a></h4>
                    </div>
                </div>
            </article>
            @endif
            @endforeach
            @endif
        </div>
    </div>
</div>
</div>
@endforeach

非常感谢您。

1 个答案:

答案 0 :(得分:0)

您不需要获取类别并单独发布。您可以使用预先加载来获取两者。为此,您可以像这样在Category模型中定义额外的关系

类别模型

public function latestPosts(){
    return $this->hasMany('App/Post')->latest()->take(6);
}

public function beforeLatestPosts(){
    return $this->hasMany('App/Post')->latest()->slice(6)->take(5);
}

获取控制器

$category_tabs = Category::with('latestPosts', 'beforeLatestPosts')->whereIn('id', [2, 3, 4])->get();

可见

打印标签页眉

<div class="collapse navbar-collapse justify-content-between" id="navbarToggler1">
    <ul class="nav nav-sm navbar-nav mr-md-auto mr-lg-0 mt-2 mt-lg-0 align-self-end" role="tablist">
        @foreach($category_tabs as $tab_category)
            <li class="nav-item">
                <a class="nav-link bg-color-tech active" id="nav-outdoor-tab-{{$tab_category->id}}" data-toggle="tab" href="#nav-outdoor-{{$tab_category->id}}" role="tab" aria-selected="true">
                    {{$tab_category->title}}
                </a>
            </li>
        @endforeach
    </ul>
</div>

打印标签内容

@foreach($category_tabs as $tab_category)
    <div class="tab-content" id="nav-tabContent">
        <div class="tab-pane fade show active" id="nav-outdoor-{{$tab_category->id}}" role="tabpanel">
            <div class="row clearfix">
                <div class="col-lg-6">
                      @foreach($tab_category->latestPosts as $latestPost)
                        <article>
                            <div class="entry-image mb-3">
                                <a href="{{route('post.show',[$latestPost->slug])}}"><img src="{{asset($latestPost->thumbnail)}}" alt="{{$latestPost->title}}"></a>
                            </div>
                            <div class="entry-title">
                                <h3><a target="_blank" href="{{route('post.show',[$latestPost->slug])}}">{{$latestPost->title}}</a></h3>
                            </div>
                            <div class="entry-content clearfix">
                                <p>{{$latestPost->description}}</p>
                            </div>
                        </article>
                      @endforeach
                </div>
                <div class="col-lg-6">
                    @foreach($tab_category->beforeLatestPosts as $beforeLatestPost)
                        <article>
                            <div class="entry-image">
                                <a href="#"><img src="{{asset($beforeLatestPost->thumbnail)}}" alt=""></a>
                            </div>
                            <div class="entry-c">
                                <div class="entry-title">
                                    <h4><a href="#">{{$beforeLatestPost->title}}</a></h4>
                                </div>
                            </div>
                        </article>
                    @endforeach
                </div>
            </div>
        </div>
    </div>
@endforeach