I’m using laravel with query builder.
Is there a way to transform a query builder result into json respecting the child elements and not creating a parse manually ?
I mean, transform this:
id customer product_id product
1 Will 1 Mouse
1 Will 2 Keyboard
into this:
{
customer: {
id: 1,
customer: will,
product: [{
id: 1,
product: Mouse
},{
id: 2,
product: Keyboard
}]
}
I have lots of code using eloquent today that needs to migrate to query builder. If I could generate a json after querying, it would salve a LOT of work changing all the other methods and views.
2
Answers
I don’t really get what you’re trying to do here
but for the start why did you had the product column if you had the product_id column? since you could just check the product name by the table relation ship
and why do you have the same id for customer id, you can’t make an id get duplicated like that
but to make a json like that, you could try this :
but I advise you to rework your database first maybe into something like this :
customer Table
| Fields | Type |
| :—– | :— |
| id | Primary Key |
| customer | Varchar |
| product_id | Foreign Key |
product Table
| Fields | Type |
| :—– | :— |
| id | Primary Key |
| product | Varchar |