Reputation: 2549
So I've found myself in a predicament. I am using Laravel in conjunction with MySQL to build a punch in/punch out website for my work. I'm doing it partially because I hate the other solutions out there and partly because I want to learn the Laravel framework. I have the punching system sorted out and everything is working, but now I'm trying to get the dashboard set up where it will list the users punches, both current and past.
In the case that a user punches in and out multiple times in one day I would like to just display the date once to make things look cleaner.
Here's an example of what I mean:
Right now all of the columns on the left-hand side have the date in them.
Now I am super new to Laravel so I know this code isn't great and I still have some fixing to do to it, but can anyone help me figure out how to group the results like in the picture above?
This is my current code:
<?php $punches = App\User::find(Auth::user()->id)->punches()->orderBy('created_at', 'desc')->get() ?>
<table class="table table-striped table-bordered">
<th>Date</th>
<th>In Time</th>
<th>Out Time</th>
@foreach($punches as $punch)
<tr>
<td>{{Carbon\Carbon::parse($punch->created_at)->format('F d, Y')}}</td>
<td>{{Carbon\Carbon::parse($punch->inPunch)->format('h:i A')}}</td>
<td>{{Carbon\Carbon::parse($punch->outPunch)->format('h:i A')}}</td>
</tr>
@endforeach
</table>
Any questions are welcome. I'll do my best to answer quickly. Please and thank you!
Upvotes: 0
Views: 189
Reputation: 4045
It does not seem that you need to group them. It is just a matter of showing when the date changes. Every time the new $punch date is different from the previous $punch display it.
<?php $current_date = null; ?>
@foreach($punches as $punch)
<tr>
<td>
@if ($punch->created_at)->format('F d, Y') != $current_date)
<?php $current_date = $punch->created_at)->format('F d, Y'); ?>
{{ $current_date }}
@endif
</td>
<td>{{Carbon\Carbon::parse($punch->inPunch)->format('h:i A')}}</td>
<td>{{Carbon\Carbon::parse($punch->outPunch)->format('h:i A')}}</td>
</tr>
@endforeach
I am sorry about the 'ugly' php code in the blade template. I wanted to show the idea of the proposed solution.
Upvotes: 2