derrickrozay's avatar

adding a whereBetween clause doesnt return expected result set

I am using Laravel 5.6 and SQLite I am trying to return certain results between a time frame. The problem is that when I add a whereBetween clause on the created_at column it is missing results. created_at is of type DateTime

Example dataset

id,created_at, status, category, c_sub_category
1, 2019-01-02 16:36:06, closed, Issue, Team 1
2, 2019-01-03 18:36:01, closed, Fix, Team 2

The startDate and endDate are carbon instances

{
    "startDate": {
        "date": "2019-01-01 00:00:00.000000",
        "timezone_type": 3,
        "timezone": "America/Toronto"
    },
    "endDate": {
        "date": "2019-01-06 23:59:59.999999",
        "timezone_type": 3,
        "timezone": "America/Toronto"
    }
}

This query should return two results. I am grouping by c_sub_category and there are only two c_sub_categories but currently it is returning just 1 result

$data  = Tickets
            ::whereBetween('created_at', [$week["startDate"], $week["endDate"]])
            ->where('status', 'closed')
            ->where('category', '!=', 'New Development')
            ->groupBy('c_sub_category')
            ->get();

This query without the whereBetween clause returning two results which is correct

$data  = Tickets
            ->where('status', 'closed')
            ->where('category', '!=', 'New Development')
            ->groupBy('c_sub_category')
            ->get();

Other ways I have tried that also don't return the correct result set

$data  = Tickets
            ::where(function ($query) use ($week) {
                $query->whereBetween('created_at', [$week["startDate"], $week["endDate"]]);
            })
            ->where('category', '!=', 'New Development')
            ->where('status', 'closed')
            ->groupBy('c_sub_category')
            ->get();

$data  = Tickets
            ::where(function ($query) use ($week) {
                $query->whereBetween('created_at', [$week["startDate"], $week["endDate"]]);
            })
            ->where(function ($query) use ($week) {
                $query->where('category', '!=', 'New Development');
                      ->where('status', 'closed');

            })
            ->groupBy('c_sub_category')
            ->get();
0 likes
1 reply
bobbybouwmann's avatar

Mmh, not sure but it might be an issue with Carbon. Instead you can do something like this

$startDate = $week['startDate']->format('Y-m-d');
$endDate = $week['endDate']->format('Y-m-d');

$data  = Tickets::whereBetween('created_at', [$startDate, $endDate]);

Let me know if this works!

Please or to participate in this conversation.