/** * Responds to requests to Process /photos/delete/{?id} */ public function getDoDelete($kid_id) { $kid = \P4\Kid::with('photos', 'users')->find($kid_id); if (is_null($kid)) { \Session::flash('flash_message', 'Kid not found.'); return redirect('\\kids'); } if ($kid->users()) { $kid->users()->detach(); } //delete photos before deleting kid record if ($kid->photos()) { //delete photo tags related records \DB::table('photo_tag')->join('photos', 'photos.id', '=', 'photo_tag.photo_id')->where('photos.kid_id', '=', $kid->id)->delete(); //delete kid's photos $kid->photos()->delete(); } //delete kid record $kid->delete(); \Session::flash('flash_message', $kid->name . ' was deleted.'); return redirect('/kids'); }
/** * Responds to requests to GET /photos for a list of photos belonging to a kid */ public function getIndex(Request $request) { // Get all the photos belonging to the selected user's kids // Sort in descending order by id $photos = \P4\Photo::with('kid')->select('photos.*')->leftJoin('kid_user', 'kid_user.kid_id', '=', 'photos.kid_id')->where('kid_user.user_id', \Auth::user()->id)->orderBy('photos.id', 'DESC')->get(); //Get a list of kids to let user know to add a kid record to start using the app $kids = []; if (sizeof($photos) == 0) { $kids = \P4\Kid::with('photos')->select('kids.*')->leftJoin('kid_user', 'kids.id', '=', 'kid_user.kid_id')->where('kid_user.user_id', \Auth::user()->id)->orderBy('kids.name', 'ASC')->get(); } return view('photos.index')->with('photos', $photos)->with('kids', $kids); }