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

markzimmer's avatar

Call to a member function getKey() on string while merging two collections

i have one eloquent query returning the below response as json [{"type":"mangoes","count":3},{"type":"bananas","count":4},{"type":"apples","count":1}]

when i try to merge the collection with a new collection below $avacado= collect(['type'=>avacado','count'=>2l]);

i get this error member function getKey()

to merge i used

$merged = $result->merge($avacado);

0 likes
6 replies
Snapey's avatar

looks more like a json object than a php collection?

farhankarim's avatar
Level 1

No need to merge.Just push the collection

$addtooldcollection=collect(['type'=>'avacado','count'=>2]);

$myoldcollection->push($addtooldcollection);

3 likes
MIS1st's avatar

@farhankarim thank worked for me but Generally this one is used for different things since its not flattering it

Please or to participate in this conversation.