public static function searchLocationTweets($locName, $locLat, $locLng)
 {
     $location = Location::where('latitude', '=', round($locLat, 7))->where('longitude', '=', round($locLng, 7))->first();
     $tweets = null;
     if ($location == null) {
         $location = new Location();
         $location->name = $locName;
         $location->latitude = round($locLat, 7);
         $location->longitude = round($locLng, 7);
         $locRet = $location->save();
         //save search history
         $search = new Search();
         $search->location_id = $location->id;
         $search->user = $_COOKIE['user'];
         $search->save();
         $tweets = self::getTweets($locName, round($locLat, 7), round($locLng, 7));
         //save tweets
         foreach ($tweets as $tweet) {
             $tweet->search_id = $search->id;
             $tweet->save();
         }
     } else {
         $search = Search::where('location_id', '=', $location->id)->orderBy('created_at', 'desc')->first();
         $searchTime = strtotime($search->created_at);
         //save new search
         $newSearch = new Search();
         $newSearch->location_id = $location->id;
         $newSearch->user = $_COOKIE['user'];
         $newSearch->save();
         //if search is older than 1 hour, tweet again
         if ($searchTime <= strtotime('-' . Config::get('app.cache_duration') . ' minutes')) {
             $tweets = self::getTweets($locName, round($locLat, 7), round($locLng, 7));
             //save tweets
             foreach ($tweets as $tweet) {
                 $tweet->search_id = $search->id;
                 $tweet->save();
             }
         } else {
             //get last search with tweets
             $search = DB::table('searches')->where('location_id', '=', $location->id)->whereExists(function ($query) {
                 $query->select(DB::raw(1))->from('tweets')->whereRaw('tweets.search_id = searches.id');
             })->orderBy('created_at', 'desc')->first();
             $tweets = Tweet::where('search_id', '=', $search->id)->get();
         }
     }
     return $tweets;
 }
 public function findDomain(Request $request)
 {
     $user = $request->user();
     $inputs = $request->only('q');
     $domains = Domain::where('domain', 'LIKE', '%' . $inputs['q'] . '%')->get();
     $search = new Search();
     $search->query = $inputs['q'];
     $search->user_id = $user->id;
     $search->save();
     foreach ($domains as $domain) {
         $result = new Searches_has_domain();
         $result->searches_id = $search->id;
         $result->domains_id = $domain->id;
         $result->save();
     }
     return response()->json($domains);
 }