public function postEditDish($id) { /* validate input */ $validator = Validator::make(Input::all(), array("name" => "required|unique:dishes", "price" => "required|integer", "description" => "required")); /* if validated */ if ($validator->passes()) { /* get input */ $dish = Dish::find($id); $dish->name = Input::get("name"); $dish->price = Input::get("price"); $dish->description = Input::get("description"); if (Input::has("new_category")) { $category = DishCategory::where("name", "=", Input::get("new_category"))->first(); if ($category) { $dish->dish_category_id = $category->id; } else { $category = new DishCategory(); $category->name = Input::get("new_category"); $category->save(); $dish->dish_category_id = $category->id; } } else { $dish->dish_category_id = Input::get("dish_category_id"); } $dish->save(); return Redirect::to("admin/dish/edit_dish/{$dish->id}")->with('message', 'Dish edited!'); } else { return Redirect::to("admin/dish/edit_dish/{$dish->id}")->withErrors($validator); } // end validation }
public function postCreateMenu() { /* validate input */ $validator = Validator::make(Input::all(), array("menu_date" => "required|date_format:Y-m-d", "dishes" => "required", "recommendation" => "required")); /* if validated */ if ($validator->passes()) { /* get input */ $menu = new Menu(); $menu->menu_date = Input::get("menu_date"); $menu->save(); $recommendation = new Recommendation(); $recommendation->menu_id = $menu->id; $recommendation->recommendation = Input::get("recommendation"); $recommendation->save(); foreach (Input::get('dishes') as $dishId) { $menu->dishes()->save(Dish::find((int) $dishId)); } return Redirect::to('admin/menu/create_menu')->with('message', 'Menu added!'); } else { return Redirect::to('admin/menu/create_menu')->withErrors($validator); } // end validation }
public function postDeleteDish($id = null) { if (!isset($id) or is_null($id)) { return Redirect::to('/admin/dishes'); } $dish = Dish::find($id); if (is_null($dish)) { return Redirect::to('/admin/dishes'); } $dish->delete(); return Redirect::to('/admin/dishes'); }
/** * add a dish order to a delivery * * @param $deliveryId * @param $dishId * @return \Illuminate\Http\RedirectResponse */ public function postAddOrder($deliveryId, $dishId) { $delivery = Delivery::find($deliveryId); $dish = Dish::find($dishId); // if delivery and dish is not loaded correctly if (!$delivery || !$dish) { return Redirect::back()->with('errors', new MessageBag(['An error has occurred.'])); } // if delivery closed if (!$delivery->is_active) { return Redirect::back()->with('errors', new MessageBag(['Delivery already closed.'])); } // place order $order = new Order(); // attach to relations $order->user()->associate(Auth::user()); $order->delivery()->associate($delivery); $order->dish()->associate($dish); $order->save(); // messages $messages = new MessageBag(); $messages->add('success', 'Successfully ordered a dish!'); $messages->add('success', 'The order has been attached to the delivery.'); return Redirect::back()->with('messages', $messages); }