Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get all items from the loop and show json

I’m using Laravel and PHP and am double looping though a list of id’s to get a list of products by id’s have have been mapped in the first loop.

Because i’m returning the response, it’s only returning the first results the list.

How can I return all the items in the list as json so I can output this from my api?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

            //Loop through and make additional calls to get all storage
            foreach ($ids as $id) {
                // Make additional API calls based on the $id
                $data = Http::get($url.'/'.$id);
                if($data->successful()) {
                   return response()->json(json_decode($data)); 
                }
            }

>Solution :

You can do:

//Loop through and make additional calls to get all storage
$results = [];

foreach ($ids as $id) {
   // Make additional API calls based on the $id
   $data = Http::get($url.'/'.$id);
   if($data->successful()) {
        $results[] = json_decode($data); 
   }
}

return response()->json($results); 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading