【发布时间】:2018-12-07 18:24:06
【问题描述】:
我有两个表用户和出席。
出席表包含 present_date 和 present_type wise 的用户出席。
present_type 保留用于轮班。如果轮班,用户可能有多个出席日期。
由于这个原因,一个用户可能在考勤表中有很多行。
我想向所有用户展示
选中=> 如果发现特定日期的出勤率并为用户键入
未选中=>如果视图 while 循环中当天不存在出勤率。
$userList = User::
orWhereHas('userAttendance', function ($q) use ($attDate,$type) {
$q->where('present_date', $attDate);
$q->wherePresentType($type);
})->get();
在刀片文件循环中:
@foreach($userList as $key => $user)
<input value="{{$user->id}}" title="" type="checkbox"
@if($user->userAttendance)
checked="checked" data-exist-id="{{$user->userAttendance->id}}"
@endif class="action-normal" />
但这并没有按预期工作。
它会检查考勤表上是否存在行,而无需过滤日期或类型写入orWhereHas Clasure!
我想检查出勤表上是否存在特定日期(如果 present_type 为不班次)的数据,而不进行其他查询以保持有限的数据库命中。
我可以通过在用户模型中创建一个函数来执行此操作,其中该函数从刀片文件 while 循环中获取参数。但我不想做额外的数据库查询。
有什么办法可以使用纯雄辩吗? 我可以使用控制器功能发送的数据在循环时检查用户吗?
【问题讨论】: