Multiply by dynamic value from JSON file while querying

Viewed 95

I have a model saving it's price in localized currency and i am storing all the currency details in a json array castled column field in the database.

my column field fee

{
  "payment_term": "Monthly",
  "min_fee": 1548,
  "max_fee": 6250,
  "currency": "EUR",
  "fee_details": null
}

I am using a cron job to download the currency rates json daily so i could use it in the webapp.

rates.json

{
    "eur": {
        "code": "EUR",
        "alphaCode": "EUR",
        "numericCode": "978",
        "name": "Euro",
        "rate": 0.85421161576795,
        "date": "Mon, 23 Aug 2021 23:55:01 GMT",
        "inverseRate": 1.1706701027485
    },
    "gbp": {
        "code": "GBP",
        "alphaCode": "GBP",
        "numericCode": "826",
        "name": "U.K. Pound Sterling",
        "rate": 0.7320321066169,
        "date": "Mon, 23 Aug 2021 23:55:01 GMT",
        "inverseRate": 1.3660603011274
    }
]

My default currency is USD and i am filtering my data based on a specific currency range powered by checkboxes

<li>
   <input id="pr-1" class="checkbox-custom" name="pr-1" type="checkbox" wire:model="fee" value="1">
   <label for="pr-1" class="checkbox-custom-label">Up to $5</label>
</li>
<li>
   <input id="pr-2" class="checkbox-custom" name="pr-2" type="checkbox" wire:model="fee" value="2">
   <label for="pr-2" class="checkbox-custom-label">$5 - $10</label>
</li>

my query

 $this->teachers = User::where('user_type','tutor')
    ->orderBy('created_at','desc')
    ->whereHas('Teacher', function($query) {
        $query->when($this->fee, function($query) {
            // get currency rates

            foreach ($this->fee as $fee_ind) {
               
               // if first range
                if ($fee_ind == '1') {
                    $min_price = 0;
                    $max_price = null;

                    // $this->rates has the decoded values from rates.json
                    
                    foreach ($this->rates as $key => $value) {
                                  
                                               
                    } 

                }
                
            }
            
            // return  $query;
        });
    })
    ->paginate(15);

Is there a way to calculate the localized prices in usd inside the loop or do i have to create a currency table with rates to achieve this ? Any help would be greatly appreciated.

EDIT: Upadted with the reading of json file

// calculate the rates
$url = storage_path('/seeds/rates_daily.json');        
$json_data = file_get_contents($url);
$this->rates = json_decode($json_data, true); 
0 Answers
Related