Laravel 5.5嵌套的foreach循环

时间:2019-02-10 06:01:04

标签: php laravel foreach nested

我有一段代码,无法循环。我希望有一个时间表,客户可以在其中跟踪他们的预订。每月。我想按月对每个预订分组。例如:1月2日预订,2月1日预订,3月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>";
    }


}

非常感谢您的帮助。

0 个答案:

没有答案