好吧..我为业务员创建了一个考勤系统,我的数据库结构是
用户(ID,名称)
出席人数(att_id,user_id,created_at)
这是我用于创建出勤的数据,
$final = array:2 [▼
0 => array:3 [▼
"id" => 6
"name" => "Talha Munshi"
"attendance" => array:4 [▼
0 => array:3 [▼
"attendance" => "P"
"advance" => "0"
"date" => Carbon {#250 ▼
+"date": "2017-07-16 08:07:00.000000"
+"timezone_type": 3
+"timezone": "Asia/Karachi"
}
]
1 => array:3 [▼
"attendance" => "P"
"advance" => "0"
"date" => Carbon {#249 ▼
+"date": "2017-07-17 08:07:00.000000"
+"timezone_type": 3
+"timezone": "Asia/Karachi"
}
]
2 => array:3 [▼
"attendance" => "A"
"advance" => "0"
"date" => Carbon {#248 ▼
+"date": "2017-07-18 08:07:00.000000"
+"timezone_type": 3
+"timezone": "Asia/Karachi"
}
]
3 => array:3 [▼
"attendance" => "L"
"advance" => "0"
"date" => Carbon {#241 ▼
+"date": "2017-07-19 08:07:00.000000"
+"timezone_type": 3
+"timezone": "Asia/Karachi"
}
]
]
]
1 => array:3 [▶]
]
$days_count = 20; //From first till today (20th)
现在,我尝试了一些编码,以垂直格式填充每天的出勤情况,视图中编写的代码是这样的:
<table class="table-responsive table-condensed table-striped table-hover table-bordered">
<thead>
<tr>
<td>Salesman</td>
<?php for($i = 1; $i <= $days_count; $i++){ ?>
<td><?php echo $i; ?></td>
<?php }?>
</tr>
</thead>
<tbody>
@forelse($final as $attend)
<tr>
<td>{{ $attend['name'] }}</td>
<?php for($i = 1; $i < $days_count; $i++){
$make_date = date("Y-m")."-".$i;
?>
<?php foreach($attend['attendance'] as $att){
if($att['date'] == $make_date){
?>
<td><?php echo $att['attendance']; ?></td>
<?php } else{?> <td>-</td> <?php } }?>
<?php }?>
</tr>
@empty
<tr><td>No Salesman</td></tr>
@endforelse
</tbody>
</table>
但是它给出的答案确实使我的大脑崩溃了。这是当前结果
有人可以帮我这个忙吗?
我通过这个做了
$final
数组。$users = User::select('id', 'name')
->where('type','LIKE','salesman')
->get();
$attendances = Attendance::whereBetween('created_at', [$first_date, $now])
->get();
foreach($users as $user)
{
$salesman['id'] = $user->id;
$salesman['name'] = $user->name;
foreach($attendances as $attendance)
{
if($attendance->user_id == $user->id)
{
$attend_2['attendance'] = $attendance->attendance;
$attend_2['advance'] = $attendance->advance;
$attend_2['date'] = $attendance->created_at;
$attend[] = $attend_2;
}
}
$salesman['attendance'] = $attend;
$final[] = $salesman;
}
必需的输出:应该是什么
最佳答案
您可以将视图更改为
<table class="table-responsive table-condensed table-striped table-hover table-bordered">
<thead>
<tr>
<td>Salesman</td>
<?php for($i = 1; $i <= $days_count; $i++){ ?>
<td>
<?php echo $i; ?>
</td>
<?php }?>
</tr>
</thead>
<tbody>
@forelse($final as $attend)
<tr>
<td>{{ $attend['name'] }}</td>
<?php
for($i = 1; $i < $days_count; $i++){
$make_date = date("Y-m")."-".$i;
$set_attendance_for_day=false;
$attendance_for_day ="-";
foreach($attend['attendance'] as $att){
if($att['date'] == $make_date){
$attendance_for_day = "P";
}
}
?>
<td>
<?php echo $attendance_for_day; ?>
</td>
<?php }?>
</tr>
@empty
<tr>
<td>No Salesman</td>
</tr>
@endforelse
</tbody>
</table>
**根据评论更新
<table class="table-responsive table-condensed table-striped table-hover table-bordered">
<thead>
<tr>
<td>Salesman</td>
<?php for($i = 1; $i <= $days_count; $i++){ ?>
<td>
<?php echo $i; ?>
</td>
<?php }?>
</tr>
</thead>
<tbody>
@forelse($final as $attend)
<tr>
<td>{{ $attend['name'] }}</td>
<?php
for($i = 1; $i < $days_count; $i++){
$make_date = date("Y-m")."-".$i;
$set_attendance_for_day=false;
$attendance_for_day ="-";
foreach($attend['attendance'] as $att){
if($att['date'] == $make_date){
$attendance_for_day = "P";
}
}
?>
<td>
<?php echo $attendance_for_day; ?>
<?php if($i==20){ ?>
---------------------------------------------------
<PLACE YOUR FORM HERE>
---------------------------------------------------
<?php } ?>
</td>
<?php }?>
</tr>
@empty
<tr>
<td>No Salesman</td>
</tr>
@endforelse
</tbody>
</table>
关于php - 在Laravel中创建出勤,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45205142/