Как отобразить несколько записей для даты в месяце, используя groupBy и laravel 7.5 - PullRequest
1 голос
/ 27 апреля 2020

Я пытаюсь отправить записи в таблицу дат и вывести описание и общее количество часов, если дата соответствует ключу. Я хочу иметь возможность отправлять несколько записей за один и тот же день и объединять описания, а также общее количество часов.

Мой метод:

    $timeRecordsArr = TimeRecord::where('time_sheet_id', $timeSheet->id)->orderBy('dateOfRecord')->get()->groupBy('dateOfRecord');
    $dataArr = array("hours"=>[], "descriptions"=>[]);

    foreach($timeRecordsArr as $timeRecords){
        $hours = 0;
        $description = "";
        foreach($timeRecords as $timeRecord){
            $hours += $timeRecord->minutes/60;
            $descriptions = $timeRecord->description;
            Log::info('hrs ' . $hours);
        }
     }
    // $mappedArray = array_keys($timeRecordArr);

Мой клинок:

@php 
    $subtotal = 0;
    $subtotal2 = 0;
@endphp
<tbody>
@for($day=1; $day<=16; $day++)

    <tr>
        <td width="20" class="center-number">{{$day}}</td>
        @if(property_exists($timeSheet->year . '-' . '0' . Carbon\Carbon::parse($timeSheet->month)->month . '-' . '0' . $day, $timeRecords))
            @foreach($timeSheet->year . '-' . '0' . Carbon\Carbon::parse($timeSheet->month)->month . '-' . '0' . $day as $record)
                <td width="180">{{ $record->description }}</td>  {{-- Concatanate Descriptions --}}
                <td width="20" class="center-number">{{ $record->minutes/60 }}</td>  {{-- Total each records hours --}}
                {{  $subtotal += $record->minutes/60 }}
            @endforeach
        @elseif(property_exists($timeSheet->year . '-' . '0' . Carbon\Carbon::parse($timeSheet->month)->month . '-' . $day, $timeRecords))
            @foreach($timeSheet->year . '-' . '0' . Carbon\Carbon::parse($timeSheet->month)->month . '-' . $day as $record)
                <td width="180">{{ $record->description }}</td>
                <td width="20" class="center-number">{{ $record->minutes/60 }}</td>
                {{  $subtotal += $record->minutes/60 }}
            @endforeach
        @else
            <td style="word-wrap:break-word;" width="180"></td>
            <td width="20" class="center-number"></td>
        @endif


        @if($day + 16 != 32)
            <td width="20" class="center-number">{{($day + 16) }}</td>
            @if(property_exists($timeSheet->year . '-' . '0' . Carbon\Carbon::parse($timeSheet->month)->month . '-' . ($day + 16), $timeRecords))
                @foreach($timeSheet->year . '-' . '0' . Carbon\Carbon::parse($timeSheet->month)->month . '-' . ($day + 16) as $record)
                    <td width="180">{{ $record->description }}</td>
                    <td width="20" class="center-number">{{ $record->minutes/60 }}</td>
                    {{  $subtotal2 +=$record->minutes/60 }}
                @endforeach
            @else
                <td style="word-wrap:break-word;" width="180"></td>
                <td width="20" class="center-number"></td>
            @endif
        @else
            <td width="20" class="center-number"></td>
            <td style="word-wrap:break-word;" width="180"></td>
            <td width="20" class="center-number"></td>
        @endif
    </tr>
@endfor

<tr>
    <td class="cell-right" colspan="2">Subtotal</td>
    <td class="center-number">{{ $subtotal }}</td>
    <td class="cell-right"  colspan="2">Subtotal</td>
    <td class="center-number">{{ $subtotal2 }}</td>
</tr>
<tr>
    <td colspan="5" class="total-for-month"><b>TOTAL FOR MONTH</b></td>
    <td class="center-number">{{ $subtotal + $subtotal2}}</td>
</tr>
</tbody>

Заранее спасибо.

...