【发布时间】: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