Пример #1
0
 /**
  * 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]);
 }
 /**
  * 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);
         }
     }
 }
 function getExample4()
 {
     // get all of the hikes
     $hikes = \App\Hike::with('peaks', 'user')->get();
     // returns 10 hikes
     foreach ($hikes as $hike) {
         // get the user
         $user = \App\User::find($hike->user_id);
         // this gets a user 10 different times, as it should
         $peaks = [];
         // get the peaks
         foreach ($hike->peaks as $peak) {
             array_push($peaks, $peak->id);
         }
         dump($peaks);
         // $user->peaks()->attach($peaks);
     }
 }
Пример #4
0
 /**
  * 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]);
 }
Пример #5
0
 /**
  * Responds to requests to GET /hikes/delete/{id}
  */
 public function getDoDelete($id)
 {
     # Get the hike to be deleted
     $hike = \App\Hike::find($id);
     if (is_null($hike)) {
         \Session::flash('flash_message', 'Hike not found.');
         return redirect('/hikes');
     } else {
         if ($hike->user_id != \Auth::id()) {
             \Session::flash('flash_message', 'Sorry; this hike doesn\'t belong to you.');
             return redirect('/hikes');
         }
     }
     # First remove any peaks associated with this hike
     if ($hike->peaks()) {
         $hike->peaks()->detach();
     }
     # Then delete the hike
     $hike->delete();
     # Done
     \Session::flash('flash_message', 'Your hike was deleted.');
     return redirect('/hikes');
 }