Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kavyajain's avatar

laravel Nested Query Builder

So the Tables goes as follows. booking_details and patient_details where patient_details has booking_id as foreign key.

$start = Carbon::parse($request->from)->format('Y-m-d');
$end = Carbon::parse($request->to)->format('Y-m-d');

$report_info = PatientDetails::with('booking')->whereHas('booking', function($query) use ($start,$end){

    $query->with('patientDetails')->whereDate('created_at','>=',$start)->WhereDate('created_at','<=',$end);
  })->whereIn('status', ["1","4"])->get();

Basically, i want to go to the bookings table and check for checkIn date from patient_details table. Please help. Thanx

0 likes
14 replies
Snapey's avatar

please help by formatting your code

Kavyajain's avatar
$start = Carbon::parse($request->from)->format('Y-m-d');

$end = Carbon::parse($request->to)->format('Y-m-d');

$report_info = PatientDetails::with('booking','masters')
                ->whereHas('booking', function($query) use ($start,$end){
                    $query->with('patientDetails')->whereDate('created_at','>=',$start)
->WhereDate('created_at','<=',$end);
                })->whereIn('status', ["1","4"])->get();

Snapey's avatar

should be ok, but my version would be

$start = Carbon::parse($request->from);

$end = Carbon::parse($request->to);

// leave dates as carbon instances

// dd($start, $end);        // check these are coming through 

$report_info = PatientDetails::with('booking','masters')
                ->whereHas('booking', function($query) use ($start,$end){
                    $query->whereDate('created_at','>=',$start)
->WhereDate('created_at','<=',$end);
                })->whereIn('status', ["1","4"])->get();
Kavyajain's avatar

Not working.

$report_info = PatientDetails::with('masters')
                    ->with(['booking' => function($query) use ($start,$end){
                             $query->whereDate('booking_details.checkIn','>=',$start)->WhereDate('booking_details.checkIn','<=',$end);
                        }])->whereIn('status', ["1","4"])->get();

So, booking_details is the table name and model is booking.

Still not working

36864's avatar

By "not working" do you mean you're getting errors, getting no data, or getting some data, but not the data you were expecting?

Kavyajain's avatar

no errors. getting some data, but not the data i was expecting.. So there is a match with that particular date sent. it is returning empty array

Kavyajain's avatar

Ok so the problem was with the status i passed.

$report_info = PatientDetails::with('masters')
                    ->with(['booking' => function($query) use ($start,$end){
                             $query->whereDate('booking_details.checkIn','>=',$start)->WhereDate('booking_details.checkIn','<=',$end);
                        }])->whereIn('status', ["1","4"])->get();

FInally woking, Thank You

Kavyajain's avatar
PatientDetails::with('masters')
                        ->with(['booking' => function($query) use ($start,$end){
                            $query->whereDate('booking_details.checkIn','>=',$start)->WhereDate('booking_details.checkIn','<=',$end);
                        }])->whereIn('status', ["0","2"])->toSql();
        print_r($report_info);

output

select * from `patient_details` where `status` in (?, ?)   

why is this happening?

Snapey's avatar

Its what you asked for.

The first query will be to get from PatientDetails where the status is 0 or 2 (code smelly 'magic numbers')

The second query will be to get Bookings that are belonging to the result of the first query and having the right date range

Eloquent will then merge the results

Snapey's avatar

Install Laravel debugbar rather than using toSQL

Kavyajain's avatar

Okie. But the problem with the query is it fetches null value to booking. I want only those records whose booking lies between check in from and to.

Cyrrently it is returning all the records with booking null if it does not match the checkin from and to date.

Snapey's avatar

Because you apply the date range to the with statement so you load all PatientDetails, but only eager load relations with the date range

If you want to restrict PatientDetails according to booking then you need to use whereHas as you had before

Kavyajain's avatar
$report_info = PatientDetails::with('booking','masters')
                ->whereHas('booking', function($query) use ($start,$end){
                    $query->whereDate('checkIn','>=',$start)
                    ->WhereDate('checkIn','<=',$end);
                })->whereIn('status', ["0","2"])->get();

This returns empty array even after having values.

Snapey's avatar
Snapey
Best Answer
Level 122

check your dates

check the checkIn column has valid values between start and end

check the checkIn column is spelled correctly and is a date or timestamp column type

Dump the sqland check what query is being run..

There are loads of things you can do to help yourself rather than asking and waiting

Please or to participate in this conversation.