/** * gets the hood_id from location string. creates table records if not exists * * @return hood object * @author gcg */ public static function fromLocation($location) { $hood = false; $location_parts = explode(",", $location); foreach ($location_parts as $index => $lp) { $location_parts[$index] = trim($lp); } try { $city = City::firstOrCreate(['name' => $location_parts[2]]); } catch (Exception $e) { Log::error('HoodModel/city', (array) $e); return false; } try { $district = District::firstOrCreate(['name' => $location_parts[1], 'city_id' => $city->id]); } catch (Exception $e) { Log::error('HoodModel/district', (array) $e); return false; } try { $hood = Hood::firstOrCreate(['name' => $location_parts[0], 'city_id' => $city->id, 'district_id' => $district->id]); } catch (Exception $e) { Log::error('HoodModel/hood', (array) $e); return false; } return $hood; }
/** * get a list of districts by city or autocomplete * * @return json * @author **/ public function getDistricts($city_id = null, $q = null) { $districts = District::orderBy('name', 'asc'); if ($q !== null and $q !== 'null') { $districts->where('name', 'LIKE', '%' . $q . '%'); } if ($city_id !== null and $city_id !== 'null') { $districts->where('city_id', $city_id); } $districts = $districts->get(); if (null === $districts) { $districts = []; } else { $districts = $districts->toArray(); } return response()->api(200, 'Districts: ', $districts); }