vendredi 2 septembre 2016

How can I do a search in Laravel 5?

I'll explain it with some details for you to understand me.

I'm trying to do a search engine in a website which is developed in Laravel 5 but truth is that I do not know how to do it. I'm following a example from a search built in Laravel 4. I'm trying to understand it because I'm still learning how to code in this Framework. I will put the code here (sorry for spanish words). This is the Model and it's called "Candidato.php".

public static function busquedaSencilla($param, $paginar = true) {
    $base = Candidato::with('tipoDocumento')->with('ultimoEstudio')->with('ultimoResultado')->leftJoin('ESTUDIOS_FORMALES AS EST', 'EST.IDCANDIDATO', '=', 'CANDIDATO.ID')
            ->leftJoin('CURSOS AS CUR', 'CUR.IDCANDIDATO', '=', 'CANDIDATO.ID')
            ->leftJoin('IDIOMA_CANDIDATO AS IDM', 'IDM.IDCANDIDATO', '=', 'CANDIDATO.ID')
            ->leftJoin('TRABAJO_CANDIDATO AS TBC', 'TBC.IDCANDIDATO', '=', 'CANDIDATO.ID')
            ->leftJoin('CARGO_CANDIDATO AS CTC', 'CTC.IDTRABAJO', '=', 'TBC.ID')
            ->leftJoin('EMPRESAS AS EMP', 'TBC.IDEMPRESA', '=', 'EMP.ID')
            ->leftJoin('IDIOMAS AS IDI', 'IDM.IDIDIOMA', '=', 'IDI.ID')
            ->leftJoin('SUB_AREA_TRABAJO AS SART', 'CTC.IDSUBAREA', '=', 'SART.ID')
            ->leftJoin('AREA_TRABAJO AS ART', 'SART.IDAREATRABAJO', '=', 'ART.ID')
            ->leftJoin('AREAS_ESTUDIO AS ARS', 'EST.IDAREAESTUDIO', '=', 'ARS.ID')
            ->leftJoin('NIVEL_DOMINIO AS NVD', 'IDM.IDNIVEL', '=', 'NVD.ID')
            ->leftJoin('NIVEL_ESTUDIOS AS NVE', 'EST.IDNIVELESTUDIO', '=', 'NVE.ID')
            ->leftJoin('INSTITUCIONES AS INS', 'EST.IDINSTITUCION', '=', 'INS.ID');
    if ($param != "") {
        $palabras = explode(" ", $param);
        foreach ($palabras as $palabra) {
            $base = $base->where(function ($query) use ($palabra) {
                foreach (Candidato::$camposBusqueda as $campo) {
                    $query = $query->orWhere('CANDIDATO.' . $campo, 'LIKE', '%' . $palabra . '%');
                }
                foreach (EstudioFormal::$camposBusqueda as $campo) {
                    $query = $query->orWhere('EST.' . $campo, 'LIKE', '%' . $palabra . '%');
                }
                foreach (Cursos::$camposBusqueda as $campo) {
                    $query = $query->orWhere('CUR.' . $campo, 'LIKE', '%' . $palabra . '%');
                }
                foreach (CargoCandidato::$camposBusqueda as $campo) {
                    $query = $query->orWhere('CTC.' . $campo, 'LIKE', '%' . $palabra . '%');
                }
                $query = $query->orWhere('NVE.NOMBNIVEL', 'LIKE', '%' . $palabra . '%');
                $query = $query->orWhere('NVD.NOMBNIVEL', 'LIKE', '%' . $palabra . '%');
                $query = $query->orWhere('ARS.NOMBAREA', 'LIKE', '%' . $palabra . '%');
                $query = $query->orWhere('ART.NOMBAREA', 'LIKE', '%' . $palabra . '%');
                $query = $query->orWhere('SART.NOMBSUBAREA', 'LIKE', '%' . $palabra . '%');
                $query = $query->orWhere('IDI.NOMBIDIOMA', 'LIKE', '%' . $palabra . '%');
                $query = $query->orWhere('INS.NOMBINSTITUCION', 'LIKE', '%' . $palabra . '%');
                $query = $query->orWhere('EMP.NOMBEMPRESA', 'LIKE', '%' . $palabra . '%');
            });
        }
    }
    $base = $base->groupBy(array('CANDIDATO.ID', 'CANDIDATO.NUMDOCUMENTO', 'CANDIDATO.NOMBRES', 'CANDIDATO.APELLIDOS', 'CANDIDATO.IDTIPODOCUMENTO', 'CANDIDATO.EMAIL', 'CANDIDATO.EMAILALT', 'CANDIDATO.TELCEL', 'CANDIDATO.TELHAB', 'CANDIDATO.EMAILVALIDO', 'CANDIDATO.FOTO', 'CANDIDATO.INDACTIVO'));
    $base = $base->orderBy('CANDIDATO.ID', 'DESC');
    $base = $base->select(array('CANDIDATO.ID', 'CANDIDATO.NUMDOCUMENTO', 'CANDIDATO.NOMBRES', 'CANDIDATO.APELLIDOS', 'CANDIDATO.IDTIPODOCUMENTO', 'CANDIDATO.EMAIL', 'CANDIDATO.EMAILALT', 'CANDIDATO.TELCEL', 'CANDIDATO.TELHAB', 'CANDIDATO.EMAILVALIDO', 'CANDIDATO.FOTO', 'CANDIDATO.INDACTIVO'))->distinct();
    if ($paginar) {
        $base = $base->simplePaginate(10);
    } else {
        $base = $base->get();
    }
    return $base;
}

Can someone explain me what do I have to know/study/review/check to do this search? I mean, it would be great if someone could explain me the code I put above. I'm looking for help and trying to find more material but a person explaning me some things at least, would be better.

Thanks all of you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire