public static function detectUserLocation() { $city = [NULL]; if (auth()->check()) { $user = auth()->user(); // $city = $user->profile->city()->get(); } else { $user_location = GeoIP::getLocation(); $city = City::where('slug', $user_location['city'])->get(); } if (empty($city[0])) { $city[0] = City::first(); } view()->share('user_city', $city[0]); }
/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request) { $this->validate($request, ['city_id' => 'integer|exists:city,id', 'tag_id' => 'integer|exists:tag,id', 'type' => 'string|in:' . implode(',', Place::types())]); if ($request->has('city_id')) { $city = City::find($request->input('city_id')); } else { $city = City::where('geoname_id', 658225)->first(); if (!count($city)) { $city = City::first(); } } $query = Place::where('city_id', $city->id); if ($request->has('search')) { $words = explode(" ", $request->input('search')); $query->whereHas('translations', function ($sub_query) use($words) { foreach ($words as $word) { // If it is Chinese, use LIKE. Else, use full text index. // http://www.regular-expressions.info/unicode.html#script if (preg_match('/\\p{Han}+/u', $word)) { $sub_query->where(function ($q) use($word) { $q->where('name', 'like', '%' . $word . '%')->orWhere('content', 'like', '%' . $word . '%'); }); } else { $sub_query->whereRaw('MATCH(name,content) AGAINST(? IN BOOLEAN MODE)', [$word . '*']); } } }); } if ($request->has('type')) { $query = $query->where('type', $request->input('type')); } if ($request->has('tag_id')) { $query = $query->whereHas('tags', function ($sub_query) use($request) { $sub_query->where('id', $request->input('tag_id')); }); } $query->with('image'); $places = $query->orderBy('like_count', 'desc')->paginate(24); return view('pages.place.index', ['places' => $places, 'city' => $city, 'type' => $request->input('type')]); }