本文介绍了在Laravel中创建出勤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯..我为业务员创建了一个考勤系统,数据库的结构是

Well.. I have created a attendance system for salesman and structure of my database is

  1. 用户(ID,名称)
  2. 出勤(att_id,user_id,created_at)
  1. users (id, name)
  2. attendances(att_id, user_id, created_at)

这是我用于创建出勤的数据,

This is the data that I have and used in creating the attendance,

$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)

现在,我尝试了一些编码以垂直格式填充每天的出勤情况,视图中编写的代码是这样的:

Now, I tried a bit of coding to populate each day's attendance in vertical format and the code written in view is this :

<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>

但是它给出的答案确实使我的大脑崩溃了.这是当前结果

But it just gave answer that literally squashed my brain. This below is the current result

有人可以帮我吗?

我通过它制作了 $ 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;
    }

必需的输出:应该是什么

Required Output: what it should be

推荐答案

您可以将视图更改为以下视图

You may change the view to following

<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>

**根据评论进行更新

** Update Based on comment

<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>

这篇关于在Laravel中创建出勤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 01:14