public function search() { $query = strtoupper(Input::get("q")); //query $team = strtoupper(Input::get("t")); //team $exclude = Input::get("e"); //id's A EXCLUIR DE LOS RESULTADOS $notInID[] = 0; $notInTR[] = 0; if (!is_null($exclude)) { //armo el arreglo con los resultados a excluir de la visual. foreach ($exclude as $e) { $notInID[] = $e; } } if ((int) $query > 0) { // BUSQUEDA POR CARNET O DOCUMENTO if ((int) $query < 999999) { // BUSQUEDA POR CARNET $socio_try = Socio::where('carnet', $query)->first(); if ($socio_try and in_array($socio_try->id, $notInID)) { // ES UN PELOTUDO Y BUSCA DE NUEVO UN JUGADOR return response()->json(['result' => 'Error', 'response' => ['title' => 'Jugador asociado', 'text' => 'El jugador <kbd>' . $socio_try->full_name . '</kbd> ya esta asociado a este club.']]); } else { $trayectoria = Trayectoria::with('socio')->with('club')->select('trayectorias.*')->join('socios', 'socios.id', '=', 'trayectorias.socio_id')->join('clubes', 'clubes.id', '=', 'trayectorias.club_id')->where('socios.carnet', $query)->whereNull('trayectorias.baja')->get(); //if (count($trayectoria)==0) { if ($trayectoria->isEmpty() or $trayectoria->first()->club_id == $team) { $results = Socio::with('persona.tipo_documento')->select('socios.*')->join('personas', 'personas.id', '=', 'socios.persona_id')->where('socios.carnet', $query)->whereNotIn('socios.id', $notInID)->limit('10')->get(); } else { return response()->json(['result' => 'Error', 'response' => ['title' => 'Jugador asociado', 'text' => 'El jugador con carnet <strong>' . $query . '</strong> (<var>' . $socio_try->full_name . '</var>) ya esta asociado a <strong>' . $trayectoria[0]->club->nombre . '</strong>']]); } } } else { //BUSQUEDA POR DOCUMENTO $socio_try = Socio::select('socios.*')->join('personas', 'personas.id', '=', 'socios.persona_id')->where('personas.documento', $query)->first(); //dd($socio_try,$notInID,in_array($socio_try->persona_id, $notInID )); if ($socio_try and in_array($socio_try->id, $notInID)) { // ES UN PELOTUDO Y BUSCA DE NUEVO UN JUGADOR return response()->json(['result' => 'Error', 'response' => ['title' => 'Jugador asociado', 'text' => 'El jugador <kbd>' . $socio_try->full_name . '</kbd> ya esta asociado a este club.']]); } else { $trayectoria = Trayectoria::with('socio.persona')->with('club')->whereNull('trayectorias.baja')->select('*')->join('socios', 'socios.id', '=', 'trayectorias.socio_id')->join('personas', 'personas.id', '=', 'socios.persona_id')->join('clubes', 'clubes.id', '=', 'trayectorias.club_id')->where("personas.documento", 'LIKE', '%' . $query . '%')->get(); //if (count($trayectoria)==0) { if ($trayectoria->isEmpty() or $trayectoria->first()->club_id == $team) { $results = Socio::with("persona.tipo_documento.pais")->select('socios.*')->join('personas', 'personas.id', '=', 'socios.persona_id')->where("documento", 'LIKE', '%' . $query . '%')->whereNotIn('socios.id', $notInID)->limit('10')->get(); } else { return response()->json(['result' => 'Error', 'response' => ['title' => 'Jugador asociado', 'text' => 'El jugador con documento <strong>' . $query . '</strong>(<var>' . $socio_try->full_name . '</var>) ya esta asociado a <strong>' . $trayectoria[0]->nombre . '</strong>']]); } } } } else { //BUSQUEDA POR NOMBRE Y APELLIDO $results = Socio::with("persona.tipo_documento.pais")->select('socios.*')->join('personas', 'personas.id', '=', 'socios.persona_id')->where(DB::raw('CONCAT(personas.nombre, " ", personas.apellido)'), 'LIKE', '%' . $query . '%')->whereNotIn('socios.id', $notInID)->orWhere(DB::raw('CONCAT(personas.apellido, " ", personas.nombre)'), 'LIKE', '%' . $query . '%')->whereNotIn('socios.id', $notInID)->limit('10')->get(); } foreach ($results as $socio) { $socio->edad_limite = $socio->edad_limite; $socio->persona->edad = $socio->persona->edad; // $socio->persona->inverse_full_name = $socio->persona->inverse_full_name; $socio->route_show = route('people.socios.show', $socio->id); } return response()->json(['result' => 'Ok', 'response' => $results]); }