i want to add 10% extra of main price of service that get by api on my laravel web application

40 Views Asked by At

In my Laravel web application, when I try to update price from provider, it updates the provider with the same price. I want to add extra 10% profit to the provider price.

My code :

public function priceUpdate($id)
{
    $provider = ApiProvider::with('services')->findOrFail($id);
    $apiLiveData = Curl::to($provider->url)->withData(['key' => $provider->api_key, 'action' => 'services'])->post();
    $currencyData = json_decode($apiLiveData);
    
    //print_r($apiLiveData);
    foreach ($provider->services as $k => $data) {
        $test = '1';
        if (isset($data->price)){
            $data->update([
                
                'api_provider_price' => collect($currencyData)->where('service', $data->api_service_id)->pluck('rate')[0] ?? $data->api_provider_price ?? $data->price,
                 'price' => collect($currencyData)->where('service', $data->api_service_id)->pluck('rate')[0] ?? $data->price
            ]); 
        }           
    }
    //return back()->with('success', 'Successfully updated');
}
1

There are 1 best solutions below

1
Hitesh Padhara On

Maybe the below solution work for you.

if (isset($data->price)) {
    $apiPrice = collect($currencyData)->where('service', $data->api_service_id)->pluck('rate')[0] ?? $data->api_provider_price ?? $data->price;
    $data->update([
         'api_provider_price' => $apiPrice,
         'price' => $apiPrice * 1.1
    ]);
}

Please let me know if this was correct or not. If not then let me know. So I will update the answer appropriately.