[英]whereBetween with registration_date in laravel
I have stored users registration data with their registration date. 我已将用户注册数据及其注册日期存储在一起。 I have two select lists in my form
我的表格中有两个选择列表
1) from - for from date 1)从-开始
2) to - for to date 2)至-迄今为止
and there is a filter button to display data as per the selected from and to values of select list. 还有一个过滤器按钮,用于显示从选择列表的值中选择的数据。
Here is my code - 这是我的代码-
if($request->from_date && $request->to_date)
{
$from=$request->from_date;
$to=$request->to_date;
$users=DB::table('registration')->whereBetween('reg_date',[$from,$to])->paginate(5);
}
It is not showing proper data. 它没有显示正确的数据。
I just want that when I click on filter button, data should be fetch as per the selected from and to values of select list. 我只是希望当我单击过滤器按钮时,应该根据选择列表中的值来获取数据。
How is this possible? 这怎么可能?
if($request->from_date && $request->to_date)
{
$from = $request->from_date;
$to = $request->to_date;
$users=DB::table('registration')->where('reg_date','>',$from)->where('reg_date','<',$to)->paginate(5);
}
Try this: 尝试这个:
$users=DB::table('registration')->where('reg_date','>',$from)->where('reg_date','<',$to)->paginate(5);
I am not sure, maybe it works only where the table field is int
or something. 我不确定,也许仅在表字段为
int
或其他内容时才有效。
You can try using whereRaw
Function of Laravel where you can enter Raw Where Clause 您可以尝试使用
whereRaw
函数,并在其中输入Raw Where子句
Try the Code Below 尝试下面的代码
if ($request->has('from_date') && $request->has('to_date')) {
$from = \Carbon\Carbon::parse($request->input('from_date'))->format('Y-m-d');
$to = \Carbon\Carbon::parse($request->input('to_date'))->format('Y-m-d');
$users=DB::table('registration')->whereRaw("DATE(`reg_date`) >= '".$from."' AND DATE(`reg_date`) <= '".$to."'")->paginate(5)
}
Just a suggestion, instead of $request->from_date
check you may use $request->has('from_date')
to check whether the parameter exists or not. 只是一个建议,而不是
$request->from_date
检查,您可以使用$request->has('from_date')
来检查参数是否存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.