0

I'm trying to get the total sum of downloaded files over all time and the last 24 hours for reporting purposes. The table I'm working on has a downloaded_at field which is a DATETIME type and a size field, which is the file size in bytes. In my model I'm doing the following queries:

return array(
    'totalBandwidth' => self::where('downloaded_at', 'IS NOT', DB::raw('null'))->sum('size'),
    'bandwidthLast24Hours' => self::where('downloaded_at', 'IS NOT', DB::raw('null'))->where('downloaded_at', '>' , new DateTime('yesterday'))->sum('size')
);

Pretty simple, however both of these queries return NULL and I can't figure out why. I've pretty much written these queries based from answers on SO and the Laravel forums.

1
  • Have you missed ->get() at the end of your queries? Commented Dec 1, 2013 at 4:42

2 Answers 2

2

That's because you can't check for IS NOT NULL like that. You gotta use the whereNotNull method. Example:

return array(
    'totalBandwidth' => self::whereNotNull('downloaded_at')->sum('size'),
    'bandwidthLast24Hours' => self::whereNotNull('downloaded_at')->where('downloaded_at', '>', new DateTime('yesterday')->sum('size')
);
Sign up to request clarification or add additional context in comments.

Comments

1

Operators usable in the where function are the same of query builder one's :

protected $operators = array(
    '=', '<', '>', '<=', '>=', '<>', '!=',
    'like', 'not like', 'between', 'ilike',
    '&', '|', '^', '<<', '>>',
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.