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

djmhdi's avatar

map array DB query

Hi friends ,how to map array DB query sql , i try mapWithKey but it work with eloquent not DB query sql. Db query gives me this result : [{"reference":"maroc1","id":1},{"reference":"maroc2","id":2},{"reference":"maroc3","id":3}]

i want this result :

{"1":"maroc1","2":"maroc2","3":"maroc3"}

I'm using laravel 5.4, Can you help me to resolve my problem ?

0 likes
2 replies
topvillas's avatar

Turn the result of your DB query into a collection.

$collection = collect($results)

djmhdi's avatar

collect gives same result as DB query

Please or to participate in this conversation.