skip to Main Content

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


  1. 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 :

    $customers = Customer::where("customer", "Will")->get();
    
    $product = [];
    
    foreach ($customers as $customer) {
        $json = [
            'id' => $customer->product_id,
            'product' => $customer->product
        ];
    
        $product[] = $json;
    }
    
    $data = [
        'id' => $customers->id,
        'customer' => $customers->customer,
        'product' => $product
    ];
    

    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 |

    Login or Signup to reply.
  2. $customers = DB::table('customers')
            ->select(
                'id', 'customer', 
                DB::raw('GROUP_CONCAT(product_id, '|', product) as products)
            )
            ->groupBy('id', 'customer')
            ->get();
    
    //if set of customers:
    $customers = CustomerResource::collect($customers)->toJson();
    //if single ($customers[0] just for take one customer, just example) customer:
    $customer = (new CustomerResource($customers[0]))->toJson();
    
    
    // app/Http/ResourcesCustomerResource.php:
    public function toArray($request)
    {
        return [
            'customer' => [
                'id' => $this->id,
                'customer' => $this->customer,
                'product' => $this->getProducts(),
            ]
        ];
    }
    
    private function getProducts(): array
    {
        $products = [];
        foreach (explode(',', $this->products) as $product) {
            $product = explode('|', $product);
            $products[] = [
                'id' => $product[0],
                'product' => $product[1]
            ]; 
        }
        
        return $products;
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search