-1

i have a laravel web application and when i try to update price form provider it update same price that already have provider i want to add extra 10% profit with provider price

anyone here who can help me

here is 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');
}
Juwel1211
  • 5
  • 3

1 Answers1

-1

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.

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Hitesh Padhara
  • 434
  • 3
  • 4
  • yes it working i need 1 more help can you please tell me if i need custom amount of profit % then how to add $profit = 20; 'price' => $apiPrice * 1.$profit like this? – Juwel1211 Mar 31 '23 at 20:15