Laravel 5.2 Foreach 按日期排序
Laravel 5.2 Foreach order by date
我创建了一个显示 Post 的视图以及对 post 的回复。对于这个问题,回复是在一个 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);
}
提前致谢!
所有 Eloquent 查询 return Laravel collections. So you can use the 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);
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
对我来说,最干净的选择是,在您的 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);
希望对您有所帮助。
我创建了一个显示 Post 的视图以及对 post 的回复。对于这个问题,回复是在一个 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);
}
提前致谢!
所有 Eloquent 查询 return Laravel collections. So you can use the 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);
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
对我来说,最干净的选择是,在您的 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);
希望对您有所帮助。