gpt4 book ai didi

mysql - 显示 LARAVEL 中按月份排序的帖子

转载 作者:行者123 更新时间:2023-11-29 19:10:28 25 4
gpt4 key购买 nike

我正在使用 Laravel 框架编写应用程序

我想要一篇关于第十个月度 View 类别的大约一年的整篇文章(每月发帖数量不是很多)

View创建了一个划分为12个部分,每个部分为一个月

我想知道如何在每个月和同月的盒子中从一系列选择中写入条目,有几个月没有任何帖子

    public function index()
{
$histories = \App\history::where('DATE','>=',$date1)->where('DATE','<=',$date2)->get();
return view ('year_posts',compact('histories'));
}

这是我想在框中显示每月帖子的页面

enter image description here

最佳答案

您可以按月对帖子进行分组,然后在边栏选项卡中迭代每个月的每个组。

Controller :

public function index()
{
$histories = \App\history::selectRaw("history.*, DATE_FORMAT(DATE, '%m-%Y') year_month")->whereBetween("DATE", [$date1, $date2])->get();

$groupedHistories = $histories->groupBy("year_month");

return view ('year_posts',compact("groupedHistories"));
}

year_posts.blade.php:

@foreach($groupedHistories as $yearMonth => $posts)
@foreach($posts as $post)
{{-- TO DO --}}
@endforeach
@endforeach

关于mysql - 显示 LARAVEL 中按月份排序的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43126220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com