mercredi 12 mai 2021

How to add JSON object into a JSON object in Laravel

I have a function which find sellers according to lat & lng and this working and return results but it not showing in response. Please help. This is what I'm executing.

    public function getProducts( $req ) {

        // ::Finding: items.
        $docs = Product::where( 'id', $req->id )->get();
        if ( !$docs->count() > 0 ) {
            return $this->Response->error( 'No data has been found.', 404 );
        }
        
        foreach ( $docs as $doc ) {
    
             foreach ( $doc->sizes as $obj ) {
                 // ::Sorting: All the available merchants according to distance.
                 $obj['merchants'] = $this->sortMerchants( $obj['id'], $req->lat, $req->lng );
             }

        }

        return $this->Response->success( 'products', $docs, $docs->count(), 200 );
    }

What I'm getting from $obj['merchants'] is :

[
  {
    "id" : "MERCHANT001",
    "details" : "Something..."
  },
  {
    "id" : "MERCHANT002",
    "details" : "Something..."
  }
]

What I'm currently getting:

"sizes": [
   {
     "id": "product-001",
     "images": ["36lzdu0UXtyDDOaZp3OKzc3hdTxfpxLtd5QH.webp"]
    }
]

What I want to achieve:

"sizes": [
   {
     "id": "product-001",
     "images": ["36lzdu0UXtyDDOaZp3OKzc3hdTxfpxLtd5QH.webp"],
     "merchants": [
        {
          "id" : "MERCHANT001",
          "details" : "Something..."
        },
        {
          "id" : "MERCHANT002",
          "details" : "Something..."
        }
     ]
  }
]


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire