Grouping MySQL results by date in PHP -
so i've found myself in predicament. using laravel in conjunction mysql build punch in/punch out website work. i'm doing partially because hate other solutions out there , partly because want learn laravel framework. have punching system sorted out , working, i'm trying dashboard set list users punches, both current , past.
in case user punches in , out multiple times in 1 day display date once make things cleaner.
here's example of mean:
right of columns on left-hand side have date in them.
now super new laravel know code isn't great , still have fixing it, can me figure out how group results in picture above?
this 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 $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 welcome. i'll best answer quickly. please , thank you!
it not seem need group them. matter of showing when date changes. every time new $punch date different previous $punch display it.
<?php $current_date = null; ?> @foreach($punches $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 sorry 'ugly' php code in blade template. wanted show idea of proposed solution.
Comments
Post a Comment