Laravel 5.2 Foreach按日期排序

时间:2017-06-19 09:45:56

标签: php laravel laravel-5 foreach laravel-5.2

我制作了一个视图,显示了帖子以及对帖子的回复。截至这个问题,回复是在一个foreach循环和随机顺序。有没有办法订购foreach循环,以便最早的回复位于顶部?最底层的回复是什么?。

这是循环

 @foreach($topic->replies as $reply)
         <div class="collection">
            <div class="collection-item row">
               <div class="col s3">
                  <div href="" class="avatar collection-link">
                     <div class="row">
                        <div class="col s3"><img src="/uploads/avatars/{{ $reply->user->avatar }}" alt="" class="circle" style="width: 50px;"></div>
                           <div class="col s9">
                              <p class="user-name">{{ $reply->user->username }}</p>
                           </div>
                        </div>
                              <p>Role: {{ $reply->user->role->role_name }}</p>
                              <p>Since: {{ $reply->user->created_at }}</p>
                              <p class="post-timestamp">Posted on: {{ $reply->created_at }}</p>
                     </div>
                   </div>
                   <div class="col s9">
                      <div class="row last-row">
                        <div class="col s12">
                           <p>{!! $reply->reply_text !!}</p>
                        </div>
                      </div>
                      <div class="row last-row block-timestamp">
                        <div class="col s6">
                           <p class="post-timestamp">Last changed: {{ $reply->updated_at }}</p>
                        </div>
                      </div>
                 </div>
             </div>
         </div>
 @endforeach

TopicsController.php(显示方法)

public function show($theme_id, $topic_id)
{
    $theme = Theme::with('topics')->findOrFail($theme_id);
    $topic = Topic::with('theme')->findOrFail($topic_id);

    return view('topics.topic')->withTopic($topic)->withTheme($theme);

}

提前致谢!

3 个答案:

答案 0 :(得分:2)

所有Eloquent查询都会返回Laravel collections。因此,您可以使用sortBy功能在foreach中订购回复。

@foreach($topic->replies->sortByDesc('created_at') as $reply)

但是,更好的解决方案是在查询中订购回复。这可以通过更新您的Eloquent查询来实现:

$topic = Topic::with([
    'theme',
    'replies' => function ($query) {
        $query->orderByDesc('created_at');
    }
])
->findOrFail($topic_id);

答案 1 :(得分:1)

public function show($theme_id, $topic_id)
{
    $theme = Theme::with([
        'topics' => function($query) {
            $query->orderBy('replies');
        }
    ])->findOrFail($theme_id);

    $topic = Topic::with('theme')->findOrFail($topic_id);

    return view('topics.topic')->withTopic($topic)->withTheme($theme);

}

https://laravel.com/docs/5.4/eloquent-relationships#constraining-eager-loads

答案 2 :(得分:0)

对我来说,最简洁的选择是,在Topic模型中,默认情况下使用created_at时间戳排序关系结果,如下所示:

class Topic
{
    public function replies()
    {
        return $this->hasMany('Reply')->orderBy('created_at','desc');
    }
}

这样你就不需要在之后订购集合了,让sql完成工作,从而产生更轻的代码和更好的性能,因为你几乎每次都需要以这种方式订购的主题。

这也有一个优点,即您共享的代码可以保持不变,但我建议您在控制器中急切加载关系(以避免执行不必要的查询):

$topic = Topic::with(['theme','replies'])->findOrFail($topic_id);

希望这会对你有所帮助。