select( 'p.park_name', 'p.update_grace_period_start_date', 'p.update_grace_period_start_time', 'p.update_grace_period_end_date', 'p.update_grace_period_end_time', 'c.city_name', 's.station_neighbor_station', 'z.psection_id' ) ->leftJoin('city as c', 'p.city_id', '=', 'c.city_id') ->leftJoin(\DB::raw( '(SELECT park_id, station_neighbor_station FROM station WHERE station_id IN (SELECT MAX(station_id) FROM station GROUP BY park_id)) as s' ),'p.park_id','=','s.park_id') ->leftJoin('zone as z', 'p.park_id', '=', 'z.park_id') //->where('p.park_name', 'a') //->where('c.city_name', 'b') //->where('s.station_neighbor_station', 'c') ->orderBy('p.park_ruby') ->get(); // 各マスタから追加情報を取得し、各ボタンの表示有無を判定する $result = []; $now = date('Y-m-d H:i:s'); foreach ($park as $row) { // ゾーンマスタの情報から空き台数を取得する $vacantInfo = \DB::table('zone') ->selectRaw('SUM(zone_tolerance) - SUM(zone_number) as vacant') ->where('psection_id', $row->psection_id) ->groupBy('psection_id') ->first(); // 定期予約マスタから予約中の台数を取得する $reservedCount = \DB::table('reserve') ->where('psection_id', $row->psection_id) ->where('valid_flag', 1) ->count(); // 空き台数を計算 $vacant = ($vacantInfo ? $vacantInfo->vacant : 0) - $reservedCount; // 更新期間内判定 $update_start = $row->update_grace_period_start_date . ' ' . $row->update_grace_period_start_time; $update_end = $row->update_grace_period_end_date . ' ' . $row->update_grace_period_end_time; $is_update_period = ($now >= $update_start && $now <= $update_end); // ボタン表示有無判定 if ($vacant > 0 && $is_update_period) { // 定期契約ボタン (空き台数が1台以上かつ更新期間内) $status = 1; } elseif ($vacant <= 0 && $is_update_period) { // 当日予約ボタン (空き台数が0台以下かつ更新期間内) $status = 2; } elseif ($vacant <= 0 && !$is_update_period) { // 販売期間外ボタン (空き台数が0台以下かつ更新期間外) $status = 3; } else { $status = null; } // 画面返却用データに追加 $result[] = [ 'park_name' => $row->park_name, 'city_name' => $row->city_name, 'station_neighbor_station' => $row->station_neighbor_station, 'status' => $status ]; } // 検索用プルダウン取得 $cities = \DB::table('city')->select('city_name')->orderBy('city_ruby')->get(); $stations = \DB::table('station')->select('station_neighbor_station')->distinct()->orderBy('station_neighbor_station')->get(); $parkings = ['全て', 'あ行', 'か行', 'さ行', 'た行', 'な行', 'は行', 'ま行', 'や行', 'ら行', 'わ行']; // 検索結果返却 return view('general.swo5_1',['form_data' => $result, 'cities' => $cities, 'stations' => $stations, 'parkings' => $parkings]); } }