/** * Responds to requests to GET / */ public function getIndex() { $public_hikes = \App\Hike::where('public', 1)->with('peaks', 'user')->orderBy('date_hiked', 'DESC')->take(10)->get(); foreach ($public_hikes as $hike) { $date_of_hike = Carbon::parse($hike->date_hiked); $hike->date_hiked = $date_of_hike->diffForHumans(); } return view('welcome')->with(['public_hikes' => $public_hikes]); }
/** * Responds to requests to GET /hikes/show/{id} */ public function getShow($id) { $hike = \App\Hike::where('id', $id)->with('peaks', 'user')->first(); if (is_null($hike)) { \Session::flash('flash_message', 'Hike not found.'); if (\Auth::check()) { return redirect('/hikes'); } else { return redirect('/'); } } $date_of_hike = Carbon::parse($hike->date_hiked); $hike->date_hiked = $date_of_hike->diffForHumans(); return view('hikes.show')->with('hike', $hike); }
/** * Run the database seeds. * * @return void */ public function run() { # First, create an array of all the hikes we want to associate peaks with # The *key* will be the hike id, and the *value* will be an array of peak ids. $hikes = [1 => [24, 34], 2 => [15, 19], 3 => [20, 40], 4 => [1, 2, 3, 4, 5, 11, 27], 5 => [14, 16], 6 => [23, 29, 39], 7 => [48], 8 => [10], 9 => [18, 25], 10 => [36]]; # Now loop through the above array, creating a new pivot for each book to tag foreach ($hikes as $hike_id => $peaks) { # First get the hike $hike = \App\Hike::where('id', 'like', $hike_id)->first(); # Now loop through each peak for this hike, adding the pivot foreach ($peaks as $peak_id) { $peak = \App\Peak::where('id', 'like', $peak_id)->first(); # Connect this peak to this hike $hike->peaks()->save($peak); } } }
/** * 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]); }