0

What I'm working on right now is a generate report and I have multiple fields in one page which are address,date,gender and vaccine

What I want is even if the address is empty and i inputted a gender it will stillretrieve all of the specific gender that has been selected and if the user has not inputted anything on the fields it will automatically retrieve all the records. So what I did is this

In my Controller

if (!$request->input) {
    $request->input = "";
}
if (!$request->vaccines) {

    $vaccine_id = "";
}else{
    $vaccine_id = $request->vaccines;
}
if (!$request->month) {

    $newDate = " ";
}else{
    $newDate = date("Y-m-d", strtotime($request->month));
}
if (!$request->gender) {

    $request->gender = "";
}

$patient = Patient::where('patient_address', 'like' ,'%' . $request->input)->where('patient_sex', 'like' ,'%' . $request->gender)->whereHas('users', function($q) use($vaccine_id){
        $q->where('vaccine_id',  $vaccine_id);
    })->get();

So I used LIKE operator so that even if the patient address was empty it will still return results unlike = operator it will automatically return false and end the rest of the WHERE clause. string type columns are working just fine like the patient address and the sex. But I just found out that you cant use a LIKE operator on integer. So if i use = on the vaccine_id, if the condition is false it will not retrieve all the data on the database automatically unlike the LIKE operator.

Is there any solution to this?

1 Answer 1

1

Try this:

$query = Patient::where('patient_address', 'like' ,'%' . $request->input)->where('patient_sex', 'like' ,'%' . $request->gender);

if($vaccine_id != "") {
    $query->whereHas('users', function($q) use($vaccine_id){
        $q->where('vaccine_id',  $vaccine_id);
    });
}

$patient = $query->get();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.