问题描述
我有一个非常简单的解决方案,但我不喜欢那样。那么,我该怎么办?
我的数据库是
--------------------------------------
id name view created_at
--------------------------------------
1 one 9 2021-01-13 12:34:22
2 two 8 2021-01-15 10:23:02
3 three 23 2021-01-15 20:55:17
4 forth 15 2021-01-16 12:34:22
5 fifth 0 2021-01-19 10:37:02
我想像这样对我的数据进行排序和获取--
--------------------------------------
id name view created_at
--------------------------------------
5 fifth 0 2021-01-19 10:37:02
3 three 23 2021-01-15 20:55:17
4 forth 15 2021-01-16 12:34:22
1 one 9 2021-01-13 12:34:22
2 two 8 2021-01-15 10:23:02
我的解决方案是
$today = '2021-01-19'; //this date will calculate in daily. Not absolute date!
$firstarray=Product::where('created_at','LIKE',$today.'%')->get();
$secondarray=Product::orderBy('viewer','DESC')->get();
$data = array_merge($firstarray,$secondarray);
return $data;
实际上,我想让我的代码像这样
$today = '2021-01-19'; //this date will calculate in daily. Not absolute date!
$data = Product::orderBy(DB::raw('FIELD(created_at,LIKE $today."%")'),'DESC')
->orderBy('view','desc')->get();
return $data;
如何为我的问题获得强大的代码?
对不起我的英语水平
谢谢大家
解决方法
我猜您首先需要今天的记录,然后是按视图排序的其余记录。所以在这种情况下,您可以将结果排序为
<trumbowyg-editor>
,
你的问题不清楚!如果这是我理解的方式,那就是解决方案。
$data = Product::whereDate('created_at',now())->orderBy('view','desc')->get();