Problems with the return of the query in Laravel 5

Asked

Viewed 110 times

0

Hello, I’m having trouble with my Laravel research 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’s a very simple function for testing, but it returns me nothing, returns an empty json, someone has some idea of the reason?

  • Within each if you need to program $mot = $mot->where('ID_CD_INTERNACAO',$request->get('id_mot')); so you haven’t worked out.

  • I just did that and I haven’t gotten the results, weird...

  • Within each if you have not updated the variable $mot with the new filter information, ie $mot = $mot->where('ID_CD_INTERNACAO',$request->get('id_mot')); assign on all rows this... Although you should still have returned something if your table contains data...

  • I already found the solution, the requests were going empty, so he wasn’t looking for anything, the question now is how to prevent him from sending the

1 answer

0

Analyzing the code, the algorithm created is correct, but it has some adjustments that can help you.

If you are passing the 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, by checking the comments, so that you can validate whether the field is empty or not, it is possible to use the Variable validation itself.

Do it that way:

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 whether the value is empty or not, if it still doesn’t work you can switch to is_null, to verify that the request is void.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.