vendredi 9 mars 2018

Laravel eloquent group by date field?

I want to group by date my select, I can do it without any problems with a int type.

Working example:

$result = Element::selectRaw('COUNT(*) as TotalItems, SUM(quantity) as Qnts)
            ->orderBy('Qnts', 'DESC')
            ->groupBy('Qnts')
            ->get();

What I tried is to use DB::raw("CAST(created_at AS Date)") in groupBy(), I get there following error message SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column...

SELECT CAST(created_at AS Date) AS DATE, SUM(quantity) as Qnts, COUNT(*) as TotalItems
FROM elements 
GROUP BY CAST(created_at AS Date)
ORDER BY Date DESC

How can I use in my eloquent select created_at field?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2oYfqIB
via IFTTT

Aucun commentaire:

Enregistrer un commentaire