У меня есть кусок кода, и у меня проблемы с его зацикливанием.Я хочу иметь график, в котором клиенты могут отслеживать свои заказы.ежемесячно.Я хочу сгруппировать каждое бронирование по месяцам.Например: 2 января, 1 февраля, 3 марта,
Модель:
public static function getHistoryByMonthClient ($client_id)
{
$ret = \App\Models\Reservation::query()
->select(\DB::raw("DATE_FORMAT(date_created, '%b %Y') as month_name"))
->where("client_id", $client_id)
//->where("reservation.status", "DONE")
->groupBy(DB::raw("MONTH(date_created)"))
->orderby('book_date')
->get();
return $ret;
}
public static function getHistoryBySingleClient ($client_id, $month)
{
$ret = \App\Models\Reservation::select('reservation.id as id', 'reservation.service_name as service_name', 'reservation.date_created as date_created',
'reservation.book_time as book_time', 'reservation.book_date as book_date','reservation.status as status', 'reservation.price as price',
'clients.first_name as first', 'clients.last_name as last', 'clients.user_email as email',
'clients.user_phonenum as phonenum', 'staffs.res_first_name as staff_first',
'staffs.res_last_name as staff_last', 'staffs.id as staff_id', 'reservation.date_created as month_year')
->join('clients', 'clients.id', '=', 'reservation.client_id')
->join('staffs', 'staffs.id', '=', 'reservation.staff_id', "left outer")
->where('reservation.client_id', $client_id)
->where(DB::raw("DATE_FORMAT('reservation.date_created', '%b %Y')",$month))
->orderby('reservation.book_date', 'DESC')
->orderby('reservation.service_name', 'ASC')
->get();
return $ret;
}
Контроллер:
public function showhistory($id)
{
$client_id = $id;
$months = \App\Models\Reservation::getHistoryByMonthClient($client_id);
$appointments = \App\Models\Reservation::getHistoryBySingleClient($client_id, $months);
if($months->isEmpty()){
echo "Client has no history of bookings";
}else{
echo "<ul class='timeline timeline-inverse'>";
foreach($months as $month) {
echo "<li class='time-label'>
<span class='bg-red'>
".$month->month_name."
</span>
</li>
";
foreach ($appointments as $appointment) {
echo "<li>
<i class='fa fa fa-calendar-plus'></i>
<div class='timeline-item'>
<span class='time'><i class='fa fa-clock-o'></i> 5 mins ago</span>
<h3 class='timeline-header no-border'>".$appointment->first." ".$appointment->last. "booked " ."on" . $appointment->book_date."
</h3>
</div>
</li>
<li>";
}
}
echo "<i class='fa fa-clock-o bg-gray'></i>
</li>
</ul>";
}
}
любая помощь очень ценится.