Problems with return of the query in laravel 5

0

Hello, I'm having problems with my laravel search function

class PesquisaController extends Controller
{
  public function pesquisaMonitoramento(Request $request, Monitoramento $mot)
  {

    $mot = $mot->newQuery();


        if($request->has('id_mot')){
            $mot->where('ID_CD_INTERNACAO',$request->get('id_mot'));
        }

        if($request->has('ds_paciente')){
            $mot->where('ID_CD_PACIENTE',$request->get('ds_paciente'));
        }

        if($request->has('ds_convenio')){
            $mot->where('ID_CD_CONVENIO',$request->get('ds_convenio'));
        }


       return $mot->get();
    }

}

It is a very simple test function, but it does not return anything, returns an empty json, does anyone have any idea of the reason?

    
asked by anonymous 10.08.2018 / 16:08

1 answer

0

By analyzing the code, the algorithm created is correct, but there are some settings that can help you.

If you are passing request information via POST, you can make the following changes:

if($request->has('id_mot')){
        $mot->where('ID_CD_INTERNACAO',$request->get('id_mot'));

Can be changed to:

if($request->id_mot){
        $mot->where('ID_CD_INTERNACAO',$request->id_mot);

Then checking the comments, so that you can validate if the field is empty or not, you can use laravel's own validation.

Do this:

public function pesquisaMonitoramento(Request $request, Monitoramento $mot)

{

$mot = $mot->newQuery();

    if(! empty($request->id_mot)){
        $mot->where('ID_CD_INTERNACAO',$request->get('id_mot'));
    }

    if(! empty($request->ds_paciente)){
        $mot->where('ID_CD_PACIENTE',$request->get('ds_paciente'));
    }

    if(! empty($request->ds_convenio)){
        $mot->where('ID_CD_CONVENIO',$request->get('ds_convenio'));
    }


   return $mot->get();
}

The function empty is used to check if the value is empty or not, if it still does not work you can change it to is_null , to check if the request is null.

    
28.08.2018 / 04:16