php ORDER BY“日期”Laravel 5 SQL 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33820002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:37:49  来源:igfitidea点击:

ORDER BY "the date" Laravel 5 SQL Query

phplaravel-5

提问by Muhammad Haryadi Futra

I want to sort the results by the date, but this Laravel 5 SQL Query is not working as i want.

我想按日期对结果进行排序,但是这个 Laravel 5 SQL 查询没有按我想要的那样工作。

$b = DB::table('entry')
                        ->select(DB::raw("DATE_FORMAT(created_at,'%d-%m-%Y') as tanggal"))
                        ->groupBy(DB::raw("DATE_FORMAT(created_at,'%d-%m-%Y')"))
                        ->orderBy(DB::raw("DATE_FORMAT(created_at,'%d-%m-%Y')"), 'asc')
                        ->get();

回答by Sulthan Allaudeen

Here's the easy way to achieve this

这是实现这一目标的简单方法

Step 1 :

第1步 :

Create a Model named as Entryby artisan command or manually

Entry通过 artisan 命令或手动创建一个名为 as 的模型

Step 2 :

第2步 :

Then from your Controller just do

然后从你的控制器做

$entry = Entry::orderBy('created_at', 'ASC')->get();

Then you should get the $entryarray of what you need.

然后你应该得到$entry你需要的数组。

Hope this helps you

希望这对你有帮助