Laravel 7:将两列日期与当前日期匹配,并根据条件显示值



离开:

id user_id start_date
1 2022-06-01 22022-06-03

如果你也在模型中定义了关系,你可以在控制器中加载约束:

$users = User::with(['leaves' => function ($query) {
$query->where('start_date', '>=', Carbon::now()))
->where('end_date', '<=', Carbon::now()));
}])->get();

这样,您就不必在刀片模板中进行查询,只需检查count($row->leaves) > 0

你尝试过这个吗:

CarbonCarbon::now()->between($row->leaves()->last()->start_date,$row->leaves()->last()->end_date) ? 'On Leave' : 'Present';

相关内容

最新更新