jenoh
jenoh

Reputation: 179

How I can groupBy by date?

I'm using Laravel 5.8. I want to make a "group by" by date in the actual month. But the format of the raw date is not just Y-m-d.

public function caGraphique () {
        $actualDate = Carbon::now('Europe/Paris');
        $ca = Order::selectRaw('sum(total_ttc) as sum, prepare_date')->groupBy('prepare_date')->where('prepare_date',[$actualDate->year.'-'.$actualDate->month])->get();
        return $ca;
    }

format of 'prepare_date' in my DB

2019-06-07 12:59:51

This returns an empty array

Upvotes: 1

Views: 228

Answers (1)

Abhay Maurya
Abhay Maurya

Reputation: 12277

Not sure what you are trying to achieve here but your query is wrong, the way you are using where statement, plus the format of provided value to match for $actualDate does not match with the one in DB either so you should modify your query to this:

$ca = Order::selectRaw('sum(total_ttc) as sum, prepare_date')->where('prepare_date', $actualDate->year.'-'.$actualDate->month.'-'.$actualDate->day)->groupBy('prepare_date')->get();

Also not sure if you are just fetching the result which matches a particular date then why do you need a group by with that ?

Anyways, I hope it helps

Upvotes: 1

Related Questions