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

insight's avatar

How to solve the issue in query ?

Dear Friends, In my query in Laravel 10 the below code

 $operatorRole = OperatorModel::whereNotIn('id',$newArray)->where('role', $verifierRole)->select('id','operator_code','role')->groupBy('id','operator_code','role')->get();

shows error as

 "message": "count(): Argument #1 ($value) must be of type Countable|array, string given",

How to use $newArray with structure

Array ( [0] => 1 [1] => 7 ) change this to ['1','7']. But still got the error .

Please advise

Thanks

Anes P A

0 likes
6 replies
Tray2's avatar

There is no count in that query, so I don't think it's that query that generates the error.

However try changing the ->get() to ->toSQL() and take a look at the generated query

Jsanwo64's avatar
Jsanwo64
Best Answer
Level 11

Try this

// Convert elements in $newArray to strings and reindex the array
$newArrayStrings = array_values(array_map('strval', $newArray));

// Use $newArrayStrings in the whereNotIn clause
$operatorRole = OperatorModel::whereNotIn('id', $newArrayStrings)
    ->where('role', $verifierRole)
    ->select('id', 'operator_code', 'role')
    ->groupBy('id', 'operator_code', 'role')
    ->get();
1 like
Jsanwo64's avatar

@insight the error is related to the count() function in your Laravel query, you can try converting the $newArray elements to strings before using them in the whereNotIn clause

Snapey's avatar

show how newArray is created. That is where the problem originates

Please or to participate in this conversation.