/** * Auxiliar function to manage the notification event. * * Everytime a film is stored, it's checked whether there * are any other films by a 1km-radius. * * If any films are found, an Event is triggered and a * notification is sent to those users. * * @param $geo coordinates * @param $user user who saved the film * @param $film film being stored */ public function sendNotification($geo, $user, $film) { // Get users' id that marked a film nearby // 1km radius $users = Film::near(1, $geo->lat, $geo->lng)->get()->unique('user_id')->pluck('user_id')->all(); // Remove authenticated user from the search if (($key = array_search($user->id, $users)) !== false) { unset($users[$key]); } // If there were any films nearby, send the notific if (!empty($users)) { Event::fire(new FilmWasStored($users, $user->id, $film)); } }
/** * Finds the nearest films. * Using the Haversine formula. * Raw Query:. * * @param int $id * * @return Response */ public function nearFilms($radius, $lat, $lng) { $films = Film::near($radius, $lat, $lng)->get(); return json_encode($films); }