Laravel hasMany relationship filter: Unknown column error

26 Views Asked by At

I'm building a scheduling app where time blocks are associated with specific classrooms. The classroom model has a hasMany relationship with the block model, which is set up correctly, according to the Laravel documentation. In this particular instance, I am trying to find rooms with empty time blocks in a specified time range. Sadly, even though it seems perfectly correct to me when looking at the docs, this implementation gives me an error when calling the controller function.

Column not found: 1054 Unknown column 'rooms.id' in 'where clause'

$rooms->whereDoesntHave('blocks', function ($query) {
    $query->where('day', '=', \request('day'))
        ->where(function ($query) {
            $query->where('startTime', '<=', \request('startTime'))
                ->where('endTime', '>', \request('startTime'));
        })
        ->orWhere(function ($query) {
            $query->where('endTime', '>', \request('endTime'))
                ->where('startTime', '<', \request('endTime'));
        })
        ->orWhere(function ($query) {
            $query->where('startTime', '>', \request('startTime'))
                ->where('endTime', '<', \request('endTime'));
        })->get();
});

$searchedRooms = $rooms->get();

This piece of code is causing me significant issues; I do some other filtering for the rooms beforehand, but that one works perfectly fine when I omit this part of the code.

0

There are 0 best solutions below