Laravel 日期比较在 Eloquent 查询中不起作用
Laravel Date comparison not working in Eloquent query
我不明白为什么但是下面的查询 return 空结果集。
due_date
是碳日期,$now=Carbon:today();
$subQuery = BillTable::where('busi_id', $business->busi_id)
->where('due_date','>=',$now)
->where('due_date','<',$now->addMonth())
->get();
另外,当我使用 whereBetween
时,它不起作用。
$subQuery = BillTable::where('busi_id', $business->busi_id)
->whereBetween('due_date',[$now, $now->addMonth()])
->get();
但是当我只是大于或小于它时它起作用
$subQuery = BillTable::where('busi_id', $business->busi_id)
->where('due_date','>',$now->addWeek())
->get();
我在这里错过了什么?
这里的问题是您对两个范围限制使用了相同的实例。当您调用 addMonth
时,您将月份添加到存储在 $now
中的实例中。下面的两个例子说明了这个问题:
1. 在两个单独的语句中使用和修改同一个变量如您所料:
$now = Carbon::now();
dump($now); // prints 2015-12-12 14:50:00.000000
dump($now->addMonth); // prints 2016-01-12 14:50:00.000000
2. 使用相同的变量并在将值传递给方法的同一语句中修改它,将以不同的方式工作,因为它会在传递给方法。这意味着两个参数将相等,因为它们都包含来自 $now
变量的相同实例,该变量在计算后将包含从现在起一个月后的 DateTime
。
$now = Carbon::now();
// Calling `addMonth` will change the value stored in `$now`
dump($now, $now->addMonth());
// The above statement prints two identical DateTime values a month from now:
// 2016-01-12 14:50:00.000000 and 2016-01-12 14:50:00.000000
这意味着您当前的代码正在检查条目是否仅正好一个月后到期。
要修复它,您需要在两个单独的变量中使用两个实例:
$from = Carbon::now();
$to = Carbon::now()->addMonth();
$subQuery = BillTable::where('busi_id', $business->busi_id)
->whereBetween('due_date',[$from, $to])
->get();
看起来是因为我在查询中使用了“$now”。
就像我在查询之前所说的那样 $now=Carbon::today();
并在查询中使用 $now
。
但后来我摆脱了它并将查询更改为使用 Carbon::today() 它起作用了。
$subQuery = BillTable::where('busi_id', $business->busi_id)
->whereBetween('due_date',[Carbon::today(), Carbon::today()->addMonth())
->get();
很奇怪。
谢谢,
K
我不明白为什么但是下面的查询 return 空结果集。
due_date
是碳日期,$now=Carbon:today();
$subQuery = BillTable::where('busi_id', $business->busi_id)
->where('due_date','>=',$now)
->where('due_date','<',$now->addMonth())
->get();
另外,当我使用 whereBetween
时,它不起作用。
$subQuery = BillTable::where('busi_id', $business->busi_id)
->whereBetween('due_date',[$now, $now->addMonth()])
->get();
但是当我只是大于或小于它时它起作用
$subQuery = BillTable::where('busi_id', $business->busi_id)
->where('due_date','>',$now->addWeek())
->get();
我在这里错过了什么?
这里的问题是您对两个范围限制使用了相同的实例。当您调用 addMonth
时,您将月份添加到存储在 $now
中的实例中。下面的两个例子说明了这个问题:
1. 在两个单独的语句中使用和修改同一个变量如您所料:
$now = Carbon::now();
dump($now); // prints 2015-12-12 14:50:00.000000
dump($now->addMonth); // prints 2016-01-12 14:50:00.000000
2. 使用相同的变量并在将值传递给方法的同一语句中修改它,将以不同的方式工作,因为它会在传递给方法。这意味着两个参数将相等,因为它们都包含来自 $now
变量的相同实例,该变量在计算后将包含从现在起一个月后的 DateTime
。
$now = Carbon::now();
// Calling `addMonth` will change the value stored in `$now`
dump($now, $now->addMonth());
// The above statement prints two identical DateTime values a month from now:
// 2016-01-12 14:50:00.000000 and 2016-01-12 14:50:00.000000
这意味着您当前的代码正在检查条目是否仅正好一个月后到期。
要修复它,您需要在两个单独的变量中使用两个实例:
$from = Carbon::now();
$to = Carbon::now()->addMonth();
$subQuery = BillTable::where('busi_id', $business->busi_id)
->whereBetween('due_date',[$from, $to])
->get();
看起来是因为我在查询中使用了“$now”。
就像我在查询之前所说的那样 $now=Carbon::today();
并在查询中使用 $now
。
但后来我摆脱了它并将查询更改为使用 Carbon::today() 它起作用了。
$subQuery = BillTable::where('busi_id', $business->busi_id)
->whereBetween('due_date',[Carbon::today(), Carbon::today()->addMonth())
->get();
很奇怪。
谢谢,
K