Creating attendance in laravel

本小妞迷上赌 提交于 2021-02-20 02:59:05

问题


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

  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

Could anyone help me out with this please ?

I made the $final array through this.

$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


回答1:


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>



回答2:


The first problem is this block

<?php foreach($attend['attendance'] as $att){ 
if($att['date'] == $make_date){
?>
<td><?php echo $att['attendance']; ?></td>
<?php } else{?> <td>-</td> <?php  } }?>
<?php }?>

You are comparing a date with the format Y-m-d with a Carbon date that was set with $attend_2['date'] = $attendance->created_at;

To fix this, you can format the Carbon date before setting it in your array: $attend_2['date'] = $attendance->created_at->format( 'Y-m-d');

The second problem is that you have for loop over the days and inside that loop, you are looping over each attendance. That is why there are so many - characters. A simple fix to this is to set a flag if the attendance is present and then check that flag outside the inside loop:

<?php for($i = 1; $i < $days_count; $i++){
    $make_date = date("Y-m")."-".$i;
    $set_attendance_for_day=false;
    foreach($attend['attendance'] as $att){
        if($att['date'] == $make_date){
            $set_attendance_for_day=true;
            ?>
            <td><?php echo $att['attendance']; ?></td>
        <?php } ?>
    <?php } ?>
    <?php if (!$set_attendance_for_day) { ?>
        <td>-</td>
    <?php } ?>
<?php }?>



回答3:


I think you have problem with the date comparison in the following line.

 if($att['date'] == $make_date){

 }

Here, $make_date will give you date something like 2017-07-20 and you are trying to compare with Carbon DateTime like 2017-07-20 04:55:09 This will never equal.

So, you have to format $att['date'] with Carbon.

Like this:

if($att['date']->format('Y-m-d') == $make_date){

}



回答4:


Try this. Remove extra loop. Create flag variable and check it.

<?php for($i = 1; $i < $days_count; $i++){
    $make_date = date("Y-m")."-".$i;
    $set_attendance_for_day=false;
    foreach($attend['attendance'] as $att){
        if($att['date'] == $make_date){
            $set_attendance_for_day=true;
            ?>
            <td><?php echo $att['attendance']; ?></td>
        <?php } ?>
    <?php } ?>
    <?php if (!$set_attendance_for_day) { ?>
        <td>-</td>
    <?php } 
       else{ ?>
        <td>P</td>
    <?php  }
    ?>
<?php }?>



回答5:


This would search if the date is present in the attendance and print accordingly

@forelse($final as $attend)
<tr>
    <td>{{ $attend['name'] }}</td>
    @for ($i = 1; $i < $days_count; $i++)
    {{--*/ $make_date = Carbon\Carbon::create(date("Y-m")."-".$i); /*--}}
    {{--*/ $key = array_search($make_date, array_column($attend['attendance'], 'date')); /*--}}
    @if ($key !== FALSE)
        <td>{{ $att['attendance'][$key]['attendance'] }}</td>
    @else
        <td>-</td>
    @endif
    @endfor
</tr>
@empty
<tr><td>No Salesman</td></tr>
@endforelse


来源:https://stackoverflow.com/questions/45205142/creating-attendance-in-laravel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!