I'm getting response from an API in Laravel using HTTP client. And It takes some time. I just want to calculate that how much percentage of data is fetched from API. And when progressbar of 100% complete it means data is completely fetched and ready to show. So any solution for this?
How to calculate percentage while getting data from API response
928 Views Asked by Hammad Butt At
2
There are 2 best solutions below
0
On
You could do some logic like:
get data from API
foreach dataset taken, do the calculation
after the calculation store/update model
get out of the foreach loop or take the next entry
or if you need to calculate average or something like that you can go
$emptyArray = [];
foreach($responses as $response) {
$emptyArray [$response->id][] = $response->data; // data === type of integer
}
$emptyArray_avg = [];
foreach($emptyArray as $arrEl) {
$emptyArray_avg[] = array_sum($arrEl)/count($arrEl);
}
Note: If there are a lot of data in the JSON response, queues would be a good solution for it :)
if you are that flexible then you can do it this way ;) You can use queue job with livewire and tailwind css to achieve this goal.
composer require livewire/livewire.php artisan make:livewire YourClassWireto create livwire class, and you can find it under /App/Http/Livewire.I hope this guide you to what you want..success