我正在尝试在Laravel中使用日期过滤器。除了start_date和end_date是相同的日期之外,其他一切都正常。然后除了我加1以外,没有其他结果。
public function userresponseReport(Request $request)
{
$data['title'] = 'User Response';
$userresponses = DB::table('user_response as g')
->select(
DB::raw('DATE(g.created_at) as created_date'),
'g.msisdn',
'g.game_code',
'g.answer',
'g.answer_code',
'g.Amount_charged',
'g.payment_ref',
'g.status',
'g.user_channel'
)
->orderByRaw('g.created_at DESC');
$render=[];
if(isset($request->start_date) && isset($request->end_date))
{
$userresponses=$userresponses->whereBetween('created_at',[$request->start_date,$request->end_date]);
$render['start_date']=$request->start_date;
$render['end_date']=$request->end_date;
}elseif(isset($request->start_date))
{
$userresponses=$userresponses->where('created_at',$request->start_date);
$render['start_date']=$request->start_date;
}
$userresponses= $userresponses->orderBy('created_at','DESC');
$userresponses= $userresponses->paginate(15);
$userresponses= $userresponses->appends($render);
$data['userresponses'] = $userresponses;
return view('report.userresponseReport',$data);
}
我该如何解决?
答案 0 :(得分:1)
您可以将whereDate()
与>=
和<=
运算符一起使用。请注意,whereDate()
方法将从datetime表达式中删除日期部分。因此,对于同一日期,查询将返回该日期的所有寄存器created_at
。
$date_from = $request->start_date;
$date_to = $request->end_date;
$render=[];
$userresponses = DB::table('user_response as g')
->select(
// ...
)
->orderByRaw('g.created_at DESC')
->when($date_from, function($query) use ($date_from, $render) {
$render['start_date'] = $date_from;
$query->whereDate('created_at', '>=', $date_from);
})
->when($date_to, function($query) use ($date_to, $render) {
$render['end_date'] = $date_to;
$query->whereDate('created_at', '<=', $date_to);
})
->orderBy('created_at','DESC')
->paginate(15);
如果要比较日期时间,请使用where()
。同样,您可以与运营商 <=
和 {{11]解决问题“如果开始日期和结束日期不在同一日期” }} 。
>=
但是很难找到从日期时间 // ...
->when($date_from, function($query) use ($date_from, $render) {
$render['start_date'] = $date_from;
$query->where('created_at', '>=', $date_from);
})
->when($date_to, function($query) use ($date_to, $render) {
$render['end_date'] = $date_to;
$query->where('created_at', '<=', $date_to);
})
到同一日期时间的记录,只有在精确的秒内创建了记录的情况下,它才会返回某些内容。
答案 1 :(得分:0)
如果开始日期和结束日期相同,您仍要返回结果?
您可以使用class ViewController: UIViewController {
let tapGesture : UIGestureRecognizer!
func viewDidLoad() {
self.tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.move(tap:)))
。
orWhere