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?
//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);