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

Kris01's avatar

Laravel Query

I have to make a query that does the following: I have a json field casted as array, I want to count the number of values inside this array and compare it with an input. Let's say user inputs 3 and in the db we have {'item1','item2','item3'}, it should return true

0 likes
4 replies
Kris01's avatar

@tykus the actual value is like this

["google","facebook","instagram"]
Kris01's avatar

@tykus Tried like this but always returns null

$query->selectRaw("JSON_LENGTH('past_deals') as x")
tykus's avatar
tykus
Best Answer
Level 104

@Kris01 okay, so it is [ and ], not { and }

I have absolutely nothing to work with as far as your Model/column name etc, so broadly the query can look like:

$count = Model::selectRaw("JSON_LENGTH(column_name, '$') as count")->value('count');

Please or to participate in this conversation.