【发布时间】:2021-02-03 15:44:42
【问题描述】:
我想在 laravel 中过滤两个日期之间的数据。我有一个带有日期列的表格,其中日期以 Y-m-d 格式存储。
$total = App\Models\Data::where('age_id',$age->id)
->where('nationality',$nationality)
->when(isset($to), function($q){
$q->whereBetween('dateTime',[$from,$to]);
})->when(!isset($to), function($q){
$q->where('dateTime',$from);
})
->sum('count');
我的条件是 $to 有时可以为空。但是 whereBetween 需要 $from 和 $to 才能工作。 whereIn 获取 $from 和 $to 的数据,但不在 $from 和 $to 之间。如果只填写 $from,我想过滤该请求日期的数据。如果 $from 和 $to 被填充,我想过滤 $from 和 $to 之间的数据并对这些数据执行计数。我怎样才能做到这一点?
【问题讨论】:
标签: laravel