コード例 #1
0
 /**
  * Display restaurants having a particular category
  *
  * @param $id
  * @return \Illuminate\View\View
  */
 public function viewAction($id)
 {
     $data = array();
     if (!$id) {
         $data['errors'][] = 'Invalid ID';
     } else {
         $data['id'] = $id;
     }
     $category = Categories::find($id);
     if (!$category) {
         $data['errors'][] = 'Category not found.';
         return view('cms.category.view', $data);
     }
     $restaurants_category = RestaurantsCategory::where('category_id', $id)->get();
     if ($restaurants_category) {
         $restaurants = array();
         foreach ($restaurants_category as $restaurant_category) {
             $restaurant = Restaurants::find($restaurant_category->restaurant_id);
             if (!$restaurant) {
                 $data['errors'][] = 'Cannot find restaurant ID ' . $restaurant->id . '.';
                 return view('cms.category.view', $data);
             }
             $restaurants[] = array('id' => $restaurant->id, 'name' => $restaurant->name, 'address' => $restaurant->address, 'telephone' => $restaurant->telephone, 'rating' => $restaurant->rating, 'budget' => $restaurant->budget);
             $restaurant = null;
         }
         $data['restaurants'] = $restaurants;
     }
     $data['page_title'] = ': ' . $category->name;
     return view('cms.category.view', $data);
 }
コード例 #2
0
 /**
  * Delete a restaurant and all data associated with it
  *
  * @param Request $request
  * @return \Illuminate\Http\RedirectResponse
  */
 public function deleteAction(Request $request)
 {
     try {
         $errors = array();
         if (!$request->isMethod('POST')) {
             throw new Exception('Must be a POST request.');
         }
         $is_ajax = $request->ajax();
         $connection = DB::connection();
         $connection->beginTransaction();
         $restaurant_id = $request->input('id');
         $restaurant = Restaurants::find($restaurant_id);
         if (!$restaurant) {
             throw new Exception('Restaurant not found');
         }
         $bookmarks = Bookmarks::where('restaurant_id', $restaurant_id);
         if ($bookmarks->get()->count()) {
             $bookmarks->delete();
         }
         $check_ins = CheckIns::where('restaurant_id', $restaurant_id);
         if ($check_ins->get()->count()) {
             $check_ins->delete();
         }
         $photos = PhotosCms::where('restaurant_id', $restaurant_id);
         if ($photos->get()->count()) {
             $photos->delete();
         }
         $restaurants_category = RestaurantsCategory::where('restaurant_id', $restaurant_id);
         if ($restaurants_category->get()->count()) {
             $restaurants_category->delete();
         }
         $reviews = Reviews::where('restaurant_id', $restaurant_id);
         if ($reviews->get()->count()) {
             $reviews->delete();
         }
         $restaurant->delete();
         $connection->commit();
         if ($is_ajax) {
             header('Content-Type: application/json');
             $success[] = 'Restaurant ID ' . $restaurant_id . ' has been deleted';
             echo json_encode(array('success' => $success));
             exit;
         } else {
             $success[] = 'Restaurant ID ' . $restaurant_id . ' has been deleted';
             \Session::flush('success', $success);
             return redirect()->back();
         }
     } catch (Exception $e) {
         $connection->rollBack();
         if ($is_ajax) {
             header('Content-Type: application/json');
             $errors[] = $e->getMessage();
             \Session::flush('errors', $errors);
             echo json_encode(array('errors' => $e->getMessage()));
             exit;
         } else {
             $errors[] = $e->getMessage();
             \Session::flush('errors', $errors);
             return redirect()->back();
         }
     }
 }