Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

HTML table head and data msmatch due to foreach laravel. how to matach it

Hello guys i have simple table which has colums

id	client_id		employee_id		         work_date		time_in_hrs
1		5						1				2023-03-28				5
2		3						2				2023-03-28				6
3		1						5				2023-03-28				7

through form the data is inserted now i need a report like below

Empoyee: EE1
SNO			client			25/03       26/03     27/03      28/03        Total
01				CLient1		5        5           6            5            21
02				CLient2		4        4           4            4            16
03				Client3		2          2          4																									

Everything is ok. how i wrote my query is

In controller

$workreports = WorksheetReport::where('employee_id',1)->orderby('wk_date', 'asc')->get();

and in my blade file the below is the thead i wrote unique so that i will get the date on htm head . this is ok

<thead>
                      <tr>                                      
                         <th>S.No</th>
                          <th>Client</th>
                           @foreach($workreports->unique('wk_date') as $workreport)
                          <th>{{$workreport->wk_date}}</th>
                          @endforeach
                           <th>Total</th>
                           </tr>
                           </thead>

This is my tbody portion in blade file. here i used groupby client name so that i can get total hrs of each client each day. when an employee works for each client daily then this code works fine. but if an employee didnt work for a client in specific date then next date hrs is filled in this date

 <tbody>
           @foreach($workreports->groupby('clientname.client_name') as $cliname=>$workreport)                                                           
            <tr>
                  <td>{{ $loop->iteration }}</td>
                   <td>{{ $cliname  ?? "-" }}</td>
                     @php $total=0;@endphp
                     @foreach($workreport as $workhrs)  // here is the issue if a client has only 1 record the foreach runs one time only. so next <td> date filled previous <td> itself  
                     <td>{{ $workhrs->wk_hrs  ?? "-" }}</td>
                     @php $total=$total + $workhrs->wk_hrs  ;@endphp
                      @endforeach                                   
                      <td>
                        {{$total}}
                          </td>
                          </tr>
                          @endforeach
                        </tbody>

you can see the out put which i mentioned above 3rd row. for client3 25 and 26 date not worked 27 and 28 worked so it filled in 25 and 26 and the total in next column. instead of this i need a "-" symbol.. how to solve this issue

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To solve this issue, you can check if the current date being looped over in the thead section exists in the current client's work reports. If it does, display the work hours for that date, otherwise display a dash (-) symbol. Here's an updated version of the tbody section in the blade file:

<tbody>
    @foreach($workreports->groupBy('clientname.client_name') as $cliname => $workreport)
        <tr>
            <td>{{ $loop->iteration }}</td>
            <td>{{ $cliname ?? "-" }}</td>
            @php $total = 0; @endphp
            @foreach($workreports->unique('wk_date') as $workdate)
                @php
                    $workhrs = $workreport->firstWhere('wk_date', $workdate->wk_date);
                @endphp
                <td>{{ $workhrs ? $workhrs->wk_hrs : "-" }}</td>
                @php $total += $workhrs ? $workhrs->wk_hrs : 0; @endphp
            @endforeach
            <td>{{ $total }}</td>
        </tr>
    @endforeach
</tbody>

In this updated version, we loop over the unique dates in the thead section instead of the work reports. Then, in the tbody section, we loop over the unique dates again and check if the current client's work reports contain a record for that date using the firstWhere method. If a record exists, we display the work hours for that date, otherwise we display a dash (-) symbol.

Please or to participate in this conversation.