7 months ago

How to fetch users who does not have reports for 6 consecutive months

Posted 7 months ago by jay_gorio

I'm developing a report system, and I want to fetch users who did not report for six (6) consecutive months. How do I achieve this?

I've tried the code below but I'm not getting the desired output. There is also a problem. Let's say the date interval is 12 months. How can I determine if there is no report for 6 consecutive months?

$dateStart = '2018-10-31';
$dateEnd = '2019-03-31';

$intervals = Carbon::parse($dateStart)->diffInMonths($dateEnd);

$users = $users->whereDoesntHave('reports', function($query) use($intervals) {

    for ($i = 5; $i >= 0; $i--) { 
        $firstMonth = Carbon::parse($dateEnd)->subMonthsNoOverflow($intervals);

        $query->where('date', '>=', $firstMonth->format('Y-m-d'))->where('date', '<=', $dateEnd);


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