Массив выглядит следующим образом:
Массив Сотрудник
Код: Выделить всё
Illuminate\Support\Collection {#1521 ▼
#items: array:13 [▼
0 => {#1520 ▼
+"name": "Employee 1"
+"employee_id": "07cdc645-b783-4855-aa7d-32fa497d8335"
}
1 => {#1523 ▼
+"name": "Employee 2"
+"employee_id": "09bea471-641b-431a-829c-324b89e030d9"
}
2 => {#1547 ▼
+"name": "Employee 3"
+"employee_id": "385fe7a3-4adb-4fa6-b630-97d61ccbd4e7"
}
Код: Выделить всё
array:3 [▼
0 => "2022-03-07"
1 => "2022-03-08"
2 => "2022-03-09"
]
Код: Выделить всё
array:24 [▼
0 => {#1920 ▼
+"id": "385fe7a3-4adb-4fa6-b630-97d61ccbd4e7"
+"employee_id": "385fe7a3-4adb-4fa6-b630-97d61ccbd4e7"
+"attendance_time": "2022-03-08 10:54:42"
+"checkin_time": "2022-03-08"
+"in_out": "in"
}
etc...
]
Что я сделал, так это сначала зациклил массив сотрудника, а затем внутри цикла добавляю еще один вложенный foreach для зацикливания массива dateRange. А затем внутри массива foreach dateRange у меня есть код для поиска данных.
Код: Выделить всё
@foreach ($employee as $emp)
{{ $emp->employee_id }}
@foreach ($dateRange as $key => $range)
@php
// Search data from array attendance where checkin_time is the same as $range
$key_attd = array_search($range, array_column(json_decode($attendance_in), 'checkin_time'));
// Search data from array attendance where checkout_timeis the same as $range
$key_attd_out = array_search($range, array_column(json_decode($attendance_out), 'checkout_time'));
// Decode json so I can use the key from result data above to search the attendance data
$decode_attendance = json_decode($attendance_in);
$decode_attendance_out = json_decode($attendance_out);
@endphp
// When key was found/not false it will get data from array attendance using the key
@if ($key_attd != false)
@if ($decode_attendance[$key_attd]->employee_id == $emp->employee_id)
{{ $decode_attendance[$key_attd]->attendance_time ?? '-' }}
@endif
@else
-
@endif
// Attendance out is the same, it will get attendance_out data using the key which is resulted from array search above
@if ($key_attd_out != false)
// When employee_id from attendance is the same as employee_id from employee
@if ($decode_attendance_out[$key_attd_out]->employee_id == $emp->employee_id)
{{ $decode_attendance_out[$key_attd_out]->attendance_time ?? '-' }}
@endif
@else
-
@endif
@endforeach
@endforeach
Я знаю, что в моем коде что-то не так, но не знаю, что именно.
Подробнее здесь: https://stackoverflow.com/questions/714 ... reach-loop
Мобильная версия