MA-2016
MA-2016

Reputation: 663

Multiple Table search query in Laravel 5.2

Trying to search Patient record from three tables in a filter...I have already write the code for patient name and patient phone but got stuck at area of treatment and doctor because both of them are in different table..Please help to write a query for this filter...

Here is my filter:

enter image description here

Patient Data along with billing(doctor and area of treatment):

enter image description here

Patient Model:

public function scopeQueryStringFilters( $query )
{

    if(request()->has('patient_name'))
    {
        $query->where('name',request('patient_name'));
    }

    if(request()->has('patient_phone'))
    {
        $query->where('phone',request('patient_phone'));
    }

}

Upvotes: 0

Views: 378

Answers (1)

Makashov Nurbol
Makashov Nurbol

Reputation: 594

You can use whereHas method.

Patient::whereHas('billing', function($query){
    $query->where('area_of_treatment_id', request('area_of_treatment_id'));
})
->get();

Patient::whereHas('billing.doctor', function($query){
    $query->where('name', request('doctor'));
})
->get();

Upvotes: 1

Related Questions