Skip to content
Advertisement

Laravel Eloquent concat and groupBy() AND return one title of each group

I have a table that contains, amongst other columns, a column of browser versions. And I simply want to know from the record-set, how many of each type of browser there are.

my code is :

JavaScript

output :

JavaScript

I want to group all browsers, without the browser version like this:

JavaScript

Advertisement

Answer

One way you can pull this off is to look for browser names in strings and parse them to just the browser name in the query. So something like Mobile Safari 15.1.2 will be parsed to Safari:

JavaScript

Please note that I have removed parts of the query builder, to make this example a little more readable. You can add back whatever you need yourself.

This will give you:

JavaScript

My test data is:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement