【问题标题】:Custom listing posts by category and nr. of posts按类别和 nr 自定义列表帖子。帖子数
【发布时间】:2017-03-02 21:46:42
【问题描述】:

希望有人可以告诉我如何完成我想做的事情。基本上我需要列出数据库中的记录,更具体地说是新闻帖子,但我需要按类别交替将帖子列为 6 个组,其中第一个帖子是特色帖子。

我需要按类别查询,在获得该类别的帖子后,我需要按 6 个帖子分组,其中第一个是特色(每个帖子都有一个列,用于标识哪个类别或特征是真还是假。

所以我想首先在我的 Category 模型中创建一个名为 posts 的方法:

public function posts(){
    return $this->hasMany('App\Post');
}

所以在我的控制器中,例如主页我可以这样做:

$categories = Category::all();

而且在我看来刀片我可以做到:

@foreach($categories as $category)
    @foreach ($category->posts() as $post)
        {{$post->title}}
    @endforeach 
@endforeach 

所以现在我需要将每个类别分成 6 个帖子,其中第一个是该类别的特色帖子。 Final Result Schema:(ex: Total categories are 4 (A,B,C,D))

```
Category A
Feature Post | Normal Post | Normal Post | Normal Post | Normal Post | Normal Post

Category B
Feature Post | Normal Post | Normal Post | Normal Post | Normal Post | Normal Post

Category C
Feature Post | Normal Post | Normal Post | Normal Post | Normal Post | Normal Post

Category D
Feature Post | Normal Post | Normal Post | Normal Post | Normal Post | Normal Post

Category A
Feature Post | Normal Post | Normal Post | Normal Post | Normal Post | Normal Post

Category B
Feature Post | Normal Post | Normal Post | Normal Post | Normal Post | Normal Post

Category C
Feature Post | Normal Post | Normal Post | Normal Post | Normal Post | Normal Post

Category D
Feature Post | Normal Post | Normal Post | Normal Post | Normal Post | Normal Post

....

有人可以提示我如何做到这一点吗?

【问题讨论】:

    标签: php laravel laravel-5 eloquent laravel-5.3


    【解决方案1】:

    我的建议是要么改变关系,要么建立新关系

    public function latestPosts(){
        return $this->hasMany('App\Post')->orderBy('featured')->take(6);
    }
    

    但是在这里,您必须确保每个类别只有一个精选帖子。如果一个类别有超过 1 个专题帖子,则所有未来的帖子都将排在第一位。

    在做

    $category->latestPosts()
    

    会给你想要的。

    【讨论】:

      【解决方案2】:

      回答您的问题:

      在您的 Category 模型中创建一个名为 postsChunck 的方法:

      public function postsChunck() 
      {
      
          return $this->posts()->orderBy('is_featured', 'DESC')->limit(6)->get();
      
      }
      

      此方法将:

      • 查询该类别的所有帖子;
      • is_featured 列排序。这应该首先显示精选的,因为 (MySql) 数据库中的布尔值存储为 0 或 1;
      • 仅获取查询的前 6 个结果。

      在您的代码中,您将使用此方法:

      @foreach($categories as $category)
          @foreach ($category->postsChunck() as $post)
              {{$post->title}}
          @endforeach 
      @endforeach 
      

      推荐

      在查询类别时,您应该预先加载其帖子,同时限制加载的帖子数量。这将减少获取每个类别的帖子所需执行的查询量。

      $categories = Category::with(['posts' => function ($query) {
      
          $query->orderBy('is_featured', 'DESC')->limit(6);
      
      }])->get();
      

      然后你的视图中就会出现你常用的代码:

      @foreach($categories as $category)
          @foreach ($category->posts as $post)
              {{$post->title}}
          @endforeach 
      @endforeach 
      

      不同之处在于我们使用的不是方法posts,而是魔术属性posts,它早先已经预先加载了帖子数据。

      【讨论】:

        【解决方案3】:

        首先,从Eager Loading 帖子开始,这样您就不会遇到为视图中的每个循环运行相关查询的 n+1 问题。

        $categories = Category::with('posts')->get();
        

        然后在您的视图中,您可以对结果进行分块并使用$loop 变量来检查类似的特征:

        @foreach($categories as $category)
            @php
                $featuredPost = $category->posts->first(function($post) {
                    return $post->featured;
                });
            @endphp
            <h1>{{$featuredPost->title}}</h1>
            <h2>FEATURED POST!</h2>
            @foreach ($category->posts->where('featured', false)->chunk(5) as $post)
                <h1>{{$post->title}}</h1>
                <h2>Not SO FEATURED POST!</h2>
            @endforeach 
        @endforeach 
        

        【讨论】:

        • 但是在精选帖子之前必须对其进行组织,因为每个帖子都有一个专栏,上面写着哪个帖子是精选的(1,0)。用你给我的例子是怎么设置的?
        • 明白了,我没听懂那部分。我已经更新它以首先反映专题帖子,然后接下来的 5 个将不是该类别的专题帖子。
        猜你喜欢
        • 2014-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-01
        • 2016-07-24
        • 1970-01-01
        • 2012-01-01
        • 1970-01-01
        相关资源
        最近更新 更多