Wrong data returned

Posted 11 months ago by ImArtur

Hi, I wrote a query to: Find all beds where beds_type = 1 and Room is SHARED (Room:SHARED equals 1) and the start_date end_date of reservations are not between two dates OR there is no reservations associated with this bed.

Query:

$bed =  (new Bed)->newQuery();
        $bed->where('bed_type', $request->type)
            ->whereHas('room', function ($query) {
            $query->where('type', Room::SHARED);
        })->whereDoesntHave('reservations', function ($query) use ($request) {
            $query->whereBetween('start_date', [$request->start, $request->end])
                ->orWhereBetween('end_date', [$request->start, $request->end]);
        })->doesntHave('reservations','or');

        return $bed->get();

Output:

{
        "id": 7,
        "bed_type": 2,
        "room_id": 22,
        "created_at": "2018-04-23 15:52:42",
        "updated_at": "2018-04-23 15:52:42"
    },
    {
        "id": 8,
        "bed_type": 1,
        "room_id": 23,
        "created_at": "2018-04-23 16:26:40",
        "updated_at": "2018-04-23 16:26:40"
    },
    {
        "id": 9,
        "bed_type": 2,
        "room_id": 23,
        "created_at": "2018-04-23 16:26:40",
        "updated_at": "2018-04-23 16:26:40"
    },
    {
        "id": 12,
        "bed_type": 1,
        "room_id": 25,
        "created_at": "2018-04-23 16:35:05",
        "updated_at": "2018-04-23 16:35:05"
    },
    {
        "id": 17,
        "bed_type": 1,
        "room_id": 28,
        "created_at": "2018-04-28 14:21:26",
        "updated_at": "2018-04-28 14:21:26"
    },
    {
        "id": 19,
        "bed_type": 2,
        "room_id": 31,
        "created_at": "2018-05-02 17:51:01",
        "updated_at": "2018-05-02 17:51:01"
    },
    {
        "id": 20,
        "bed_type": 2,
        "room_id": 32,
        "created_at": "2018-05-08 10:21:18",
        "updated_at": "2018-05-08 10:21:18"
    }

Can you guys tell my why results return beds with bed_type 2? I find out that it looks like my query where is overwritten by whereDoesntHave or doesntHave

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.