Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GroundZero's avatar

SUM and GroupBy problems / guiding

Ha guys,

I am currently learning Eloquent but I am having trouble forming my query properly. What I want to archieve is:

Get ALL results but group on "url" and count each value togather from "page_load_time" and "pagespeed_score" field.

So basicly I have this date:


1          0.214           9.2              xxx.it/mypage/
2          0.321           8.9              xxx.it/mypage/

and it should become this:

1          0.535            18.1               xxx.it/mypage/

My current query is:

$data = Rapports::select('id', DB::raw('SUM(page_load_time) as pltt'), 'uri', DB::raw('SUM(pagespeed_score) as pst'), 'execution_date')
                        ->orderBy('id', 'desc')
                        ->where('execution_date', '>=', date("Y", time()).'-'.date("m", time()).'-01')
                        ->whereIn('uri', $arr)
                        ->groupBy('uri')
                        ->get();

Besides that, I cannot use groupBy without pasting in ALL my fields but I only want to group on "url" and not everything (which isnt working anyways lol).

In normal SQL I just write "GROUP BY url" and thats it, all results are grouped. But Laravel 5.4 gives me an error when I do this and forces me to enter ALL row names into the groupBy.

Hope someone can give me the right answer :)

Your sincerely,

Mithradantor

0 likes
1 reply
GroundZero's avatar

I have just found the answer after hours of searching. I am a total noob and still learning Laravel so yeah ^^

Anyways the answer to my problem:

open the database.php file in /config/database.php and find your database connection. It should have a line saying "strict". Standard this is set to "true" which forces you to parse all row names.

When set to "false" you can use groupBy with just one row name i.e:

'strict' => false,

Hope this will help out others as well when they encounter this "problem".

Thanks

Please or to participate in this conversation.