【问题标题】:Is there any way to loop 2 foreach and one for loop together?有没有办法将 2 个 foreach 和一个 for 循环一起循环?
【发布时间】:2021-04-22 06:13:22
【问题描述】:

我正在开发 Laravel 8 项目,我将在其中创建人力资源管理系统,所以我现在很难报告员工出勤率。 我有 2 个模型 User.php 和 Attendance.php 也是 2 个控制器和模型之间的关系。

public function attendances()
{
    return $this->hasMany(Attendance::class, 'user_id');
}

这是来自我的AttendanceController.php

public function index()
{
    return view('frontend.attendances.index', [
        'employees' => $this->userRepository->getAllUsers()
            ->with(['attendances' => function($query) {
                $query->where('company_id', Auth::user()->company_id);
            }])
            ->get()
    ]);
}

所以现在我必须显示包含当前月份所有日期的表格,并显示每个用户是否在工作。 我有这是我的summary.blade.php

@foreach($employees as $employee)
    <tr>
        <td>
            <img class="img-fluid rounded-circle mr-2" width="30" height="30" src="{{ $employee->image ? $employee->image : asset('assets/images/user.jpg') }}" alt="{{ $employee->firstname }}" data-original-title="{{ $employee->firstname }}">
            {{ $employee->name() }}
        </td>
        @php($count=0)
        @for($i=1; Carbon\Carbon::now()->daysInMonth >= $i; $i++)
            @foreach($employee->attendances as $attendance)
                @if($i == Carbon\Carbon::createFromFormat('Y-m-d H:m:s', $attendance->check_in)->isoFormat('D'))
                    <td class="text-center"><i class="fa fa-check text-success"></i></td>
                    @php($count++)
                @else
                    <td class="text-center"><i class="fa fa-times text-danger"></i></td>
                @endif
            @endforeach
        @endfor
        <td class="text-success text-center">{{ $count }} / {{ Carbon\Carbon::now()->daysInMonth }}</td>
    </tr>
@endforeach

这不是它应该如何工作的方式,但我已经尝试了很多方式,但仍然找不到合适的方式。 这是我想要实现的目标以及我的代码所拥有的图片

【问题讨论】:

  • 有什么问题?它应该显示什么?
  • 它应该显示图像上的数据。使用当前循环,它会为每个出席者继续&lt;td&gt;...基本上循环从 1 月 1 日到 31 日,只显示一个出席者,之后它再次继续并显示第二个出席者等等......所以我的 1 月没有t 在第 31 天结束......它将与我的出席人数一样多(31 x 出席人数)我已经用图片更新了问题,我想要实现什么以及我拥有什么
  • 我认为这是两个问题的组合:1)您可能没有相关月份的每一天的 Attendance 和 2)您正在加载 $employee-&gt;attendances 关系中的每个出席(即使是那些与当月无关的)。要确定,需要更多关于 Attendance 模型的详细信息(迁移、强制转换)

标签: php arrays laravel laravel-blade laravel-collection


【解决方案1】:

这样的事情怎么样:遍历日期并根据日期过滤员工的出勤率。

//First, create a date period. 

//use Carbon\Carbon; use Carbon\CarbonPeriod;
$daysOfThisMonth = CarbonPeriod::create(Carbon::now()->startOfMonth(),  Carbon::now()->endOfMonth());
 
//Loop through the days 
foreach($daysOfThisMonth as $day){
 //firstWhere will return one row if day exists in check_in field (equivalent to true), otherwise will return nothing, which will be equivalent to a false.
 if( $employee->attendance->firstWhere('check_in', $day ) ) //formatthe dates according to your preference
   echo "attended - print tick";
 else
   echo "not attended - print x";
}

【讨论】:

  • 正是我想要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-04
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
相关资源
最近更新 更多