我试图创建一个报告页面,显示从特定日期到特定日期的报告。这是我当前的代码:
$now = date('Y-m-d');
$reservations = Reservation::where('reservation_from', $now)->get();
这在普通SQL中所做的是从表中选择*,其中reservation_from = $now。
我在这里有这个问题,但我不知道如何将其转换为雄辩的问题。
SELECT * FROM table WHERE reservation_from BETWEEN '$from' AND '$to
如何将上面的代码转换为雄辩的查询?
让我用准确的时间戳添加正确的语法
之前
$from = $request->from;
$to = $request->to;
Reservation::whereBetween('reservation_from', [$from, $to])->get();
后
$from = date('Y-m-d', strtotime($request->from));
$to = date('Y-m-d', strtotime($request->to));
Reservation::whereBetween('reservation_from', [$from, $to])->get();
注意:如果您以字符串形式存储日期,那么请确保在from和to中传递准确的格式。它将与DB匹配。
让我用准确的时间戳添加正确的语法
之前
$from = $request->from;
$to = $request->to;
Reservation::whereBetween('reservation_from', [$from, $to])->get();
后
$from = date('Y-m-d', strtotime($request->from));
$to = date('Y-m-d', strtotime($request->to));
Reservation::whereBetween('reservation_from', [$from, $to])->get();
注意:如果您以字符串形式存储日期,那么请确保在from和to中传递准确的格式。它将与DB匹配。