subDays($purgeDays); User::query()->where('roles_id', 1)->where(function ($q) use ($threshold) { $q->where(function ($qq) use ($threshold) { $qq->whereNotNull('lastlogin')->where('lastlogin', '<', $threshold); })->orWhere(function ($qq) use ($threshold) { // Only treat null lastlogin as inactive if the account is older than threshold $qq->whereNull('lastlogin')->where('created_at', '<', $threshold); }); })->where(function ($q) use ($threshold) { $q->where(function ($qq) use ($threshold) { $qq->whereNotNull('apiaccess')->where('apiaccess', '<', $threshold); })->orWhere(function ($qq) use ($threshold) { $qq->whereNull('apiaccess')->where('created_at', '<', $threshold); }); }); } }