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

dillscher's avatar

Can I group by a json property?

Hi there,

me again ;-). Can I make an eloquent query to group by a json column property like:

Model::get()->groupBy('column->property')

? Does not seem to work, but maybe it does not work at all?

Cheers

Chris

0 likes
6 replies
crnkovic's avatar
crnkovic
Best Answer
Level 43

Maybe do this:

Model::select('column->property as field')
    ->...
    ->groupBy('field');
2 likes
dillscher's avatar

Hey crnkovic,

many thanks :-). That did work as expected.

maximos's avatar

Hi) How i can select all distinct values from json array? My structure is: id, items. Items contains json array like ['Apple', 'Orange'], ['Apple', 'Juice', 'Tomato']. I want select all distinct, i.e. Apple, Orange, Juice, Tomato.

Please or to participate in this conversation.