/** * Responds to requests to GET /peaks */ public function getIndex() { $peaks = \App\Peak::orderBy('elevation', 'DESC')->get(); $peaks_summitted = []; if (\Auth::check()) { $hikes = \App\Hike::where('user_id', \Auth::id())->with('peaks')->orderBy('date_hiked', 'DESC')->get(); $user_peaks = []; foreach ($hikes as $hike) { foreach ($hike->peaks as $peak) { array_push($user_peaks, $peak->id); } } $user_peaks = array_unique($user_peaks); foreach ($user_peaks as $peak) { array_push($peaks_summitted, $peak); } } foreach ($peaks as $peak) { if (in_array($peak->id, $peaks_summitted)) { $peak->summitted = 1; } } return view('peaks.index')->with(['peaks' => $peaks, 'peaks_summitted' => $peaks_summitted]); }