0

I'm working on data assembling in a Laravel project. The data I could get from sql query is like this

[
  {
    "id": 1,
    "name": "John"
  },
  {
    "id": 2,
    "name": "Ben"
  }
]

I want to convert it to something like this,

{
  "1":{
    "name": "John"
  },
  "2":{
    "name": "Ben"
  }
}

Anyway elegant way to do it?

3
  • any code before elegance? Commented Mar 7, 2018 at 16:58
  • @codekaizer I used groupBy('id') Commented Mar 7, 2018 at 16:58
  • Show your code in the question. You can edit your question. Comments are not for question details. Commented Mar 7, 2018 at 19:04

1 Answer 1

1

Use the keyBy collection method

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.