/** * check if there is a barangay and if it already exists in the database * if not, add it to the database * * @param Request $request * @param $site * * @return bool|int */ protected function _checkNewBarangay(Request $request, $site) { if (isset($request['barangay_name']) && $request['barangay_name'] != '') { $barangay_model = new Barangay(); $request['barangay_name'] = ucfirst(strtolower($request['barangay_name'])); if (!($barangay = $barangay_model->whereName($request['barangay_name'])->whereCityId($site->city_id)->first(['id']))) { $barangay = Barangay::create(['name' => $request['barangay_name'], 'city_id' => $site->city_id]); } return $barangay->id; } return false; }
/** * @param $city * * @return \Illuminate\Http\JsonResponse */ public function barangays($city) { $b = new Barangay(); $barangays = $b->whereCityId($city)->orderBy('name')->get(['id', 'name']); return response()->json(['type' => 'success', 'message' => 'success', 'data' => $barangays]); }
/** * @return mixed|string */ private function _checkBarangay() { if (!empty($this->request['add_barangay'])) { $this->request['add_barangay'] = ucwords(strtolower(trim($this->request['add_barangay']))); if (!($this->request['barangay_id'] = Barangay::where([['city_id', $this->request['city_id']], ['name', $this->request['add_barangay']]])->get()->first())) { $id = $this->request['barangay_id'] = Barangay::insertGetId(['name' => $this->request['add_barangay'], 'slug' => str_slug($this->request['add_barangay']), 'city_id' => $this->request['city_id']]); $this->request['barangay_id'] = $id; } return $this->request['barangay_id']; } return ''; }