2

I need to sum values using Laravel db::table and get the result between two dates, example:

Date: 05/30/2016 Sold: 1 Date: 05/31/2016 Sold: 2

So result should be Sold: 3

This is a search result given by a filter by date, i have done something like this with no results:

DB::table('sales')
 ->where('date',$date_from)
 ->where('date',$date_to)
 ->selectRaw('sum(price)')
 ->selectRaw('id')
 ->groupBy('id')
 ->get(); 

3 Answers 3

1

You Should use whereBetween(), Try this one

DB::table('sales')
->whereBetween('date', [$date_from, $date_to])
->selectRaw('sum(price)')
->selectRaw('id')
->groupBy('id')
->get(); 
Sign up to request clarification or add additional context in comments.

Comments

1

Use < and > to filter results by a date:

->where('date', '>', $date_from)
->where('date', '<', $date_to)

Or use whereBetween():

->whereBetween('date', [$date_from, $date_to])

1 Comment

Hello, i have tried this: $date_from = '2016-09-25 00:00:00.000000'; $date_to = '2016-09-26 00:00:00.000000'; ->where('date', '>', $date_from ) ->where('date', '<', $date_to ) And with Laravel im getting empty results, but when using common sql sentence on phpmyadmin im getting values.
1

You should use whereBetween function, and I think you are using a wrong group by:

DB::table('sales')
        ->whereBetween('date', [$date_from, $date_to])
        ->groupBy('date')
        ->sum('price');

And if the date column's type is a `DATETIME, you should use:

->groupBy(DB::raw("DATE(date)"))

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.