/** * * @param $name * @return Food */ public function findOrInsertFoodIfNotExists($name) { $food = Food::forCurrentUser()->where('name', $name)->first(); if (!$food) { $food = $this->foodsRepository->insert($name); } return $food; }
/** * GET api/foods * @param Request $request * @return mixed */ public function index(Request $request) { if ($request->get('typing')) { $foods = Food::forCurrentUser()->where('name', 'LIKE', '%' . $request->get('typing') . '%')->with('units')->get(); return $this->transform($this->createCollection($foods, new FoodTransformer()), ['units']); } return $this->foodsRepository->getFoods(); }