Home » Php » php – Laravel : convert grouped data to one row – Stack Overflow

php – Laravel : convert grouped data to one row – Stack Overflow

Posted by: admin February 20, 2020 Leave a comment

Questions:

Hello please i want to convert grouped data with duplicated keys and different values to one row.
For example :

"book1" {
    "id" : "1",
    "group" : "book1",
    "name" : "Book X",
    "buy" : "null",
    "test" : "null",
} ,
"book1" {
    "id" : "1",
    "group" : "book1",
    "name" : "null",
    "buy" : "100",
    "test" : "null",
} ,
"book1" {
    "id" : "1",
    "group" : "book1",
    "name" : "null",
    "buy" : "null",
    "test" : "344",
},
"book2" {
    "id" : "1",
    "group" : "book2",
    "name" : "Book Y",
    "buy" : "null",
    "test" : "null",
} 
...

what i want to do is convert all this arrays with common key to one array or one row in the database where data are grouped like the example the data are grouped by group field
and what i want to do is like that :

{
  "id" : "1",
  "group" : "book1",
  "name" : "Book X",
  "buy" : "100",
  "test" : "344",
}

Please could you help to fix by php code (laravel model) or SQL query and thank you so much !

Answers: