在whereBetween查询中将laravel中的created_at列转换为y-m-d格式 [英] convert created_at column in laravel to y-m-d format in whereBetween query
问题描述
这是我的控制器
public function index(){
$products = Order::whereHas('user', function ($query) {
if(request()->has('d') && request()->get('d')){
$arr = [
'start' => Carbon::parse(substr(request()->get('d'), 4, 11))->format('Y-m-d'),
'end' => Carbon::parse(substr(request()->get('d'), 64, -44))->format('Y-m-d')
];
$builder = $query->whereBetween('created_at', [$arr['start'],$arr['end']]);
}
});
return OrdersResource::collection($orders->latest()->paginate(5));
}
我的数组$arr
看起来像这样
array:2 [
"start" => "2018-08-07"
"end" => "2018-08-09"
]
如何将laravel中的created_at (which is a datetime)
列更改为仅年月日格式
How can i change the created_at (which is a datetime)
column in laravel to only Year month day format
我需要将created_at转换为该格式,以便执行laravel的whereBetween查询,因为它没有提供确切的结果
I need to convert the created_at to that format in order to perform the whereBetween query of laravel because it didn't give me the exact result
推荐答案
我建议您将start
和end
更新为第一个日期的开始,而不是更改created_at
列的格式.最后一个日期的结束. Carbon
对象应该传递给whereBetween
方法,而不是它们的字符串表示形式.
Instead of change the format of the created_at
column, I suggest that you should update the start
and end
to beginning of the first date and the ending of the last date. The Carbon
objects should be passed to the whereBetween
method instead of their string representation.
$arr = [
'start' => Carbon::parse(substr(request()->get('d'), 4, 11))->startOfDay(),
'end' => Carbon::parse(substr(request()->get('d'), 64, -44))->endOfDay()
];
// Both `start` and `end` are Carbon objects
$builder = $query->whereBetween('created_at', [$arr['start'],$arr['end']]);
这篇关于在whereBetween查询中将laravel中的created_at列转换为y-m-d格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!