【问题标题】:Laravel date range not working with eloquentLaravel 日期范围不适用于 eloquent
【发布时间】:2021-03-12 08:50:12
【问题描述】:

您好,我有一列名称作为日期,它将保留预付款日期。我想获取当前月份的预付款。对于我在下面的代码中使用的内容。

echo $startDate = date($currentYear . "-" . $currentMonth . "-1");
echo $endDate = date($currentYear . "-" . $currentMonth . "-" . $numberOfDaysInMonth);
DB::enableQueryLog();
echo $advances = UserServiceAdvancePayment::where('user_service_master_id', $userServiceMasterObject->id)
    ->whereDate('date', ">=", $startDate)
    ->whereDate('date', "<", $endDate)
    ->get();

这里在应用一个日期条件时获取数据,但在应用另一个日期条件时它不返回数据。也尝试过同样的问题。对此的任何帮助深表感谢

【问题讨论】:

  • 您的日期是否正确?我没有看到任何错误,您的查询是如何生成的?
  • select * from "user_service_advance_payments" where "user_service_master_id" = ? and date >= '2021-03-1' and date

标签: php laravel date


【解决方案1】:

如果我理解您需要正确执行的操作,那么使用 Carbon 会更容易:


$startDate = CarbonImmutable::createFromDate($currentYear, $currentMonth, 1)
$endDate = CarbonImmutable::createFromDate($currentYear, $currentMonth, 1)->endOfMonth();
DB::enableQueryLog();
$advances = UserServiceAdvancePayment::where('user_service_master_id', $userServiceMasterObject->id)
    ->whereDate('date', ">=", $startDate)
    ->whereDate('date', "<=", $endDate)
    ->get();

【讨论】:

  • 感谢您的回复,但仍然没有输出。
猜你喜欢
  • 2017-08-28
  • 2012-01-05
  • 2023-02-08
  • 2019-05-08
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多