我有一张名为“posts”的表格,包含以下列:“post_id int primary increments”、“poster_id int”和“status text”,还有一个名为“friends”的数组,包括以下列:“user_id int primary”和“friend_ids text”。
我需要获取在“friends”的文本列中的所有ID,这很容易使用以下方法:
$friends = explode(',', \Friend::where('user_id', \Sentry::getUser()->id)->first()->friend_ids);
文本列中的数据看起来像 '1,2,3,' 等。
然后,我创建一个 Eloquent Collection 对象,这也可以很容易地通过以下方式完成:
$posts = new \Illuminate\Database\Eloquent\Collection();
但问题是我无法弄清楚如何填充集合并按照帖子对象的“created_at”列对其内容进行排序。
目前我的代码如下:
foreach ($friends as $id) {
$posts_ = \Post::where('poster_id', $id)->getQuery()
->orderBy('created_at', 'desc')
->get();
foreach($posts_ as $post) {
$posts->add($post);
}
}
我无法确定这段代码是否可以按照“created_at”列对所有文章进行排序,同时我也需要能够轻松地对整个集合进行分页。
什么是推荐的集合排序方式?
return $post->type->hierarchy;
,然后它就能工作了! - Pathrosreturn strtotime($post->created_at);
。 - Altrim