/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { if (!$this->fetcher->isTodaysCurrencyRatesFetched()) { $this->fetcher->fetchLatestAndUpdate(); } $rates = CurrencyRate::with('currency')->latest()->orderBy('currency_id')->get(); if ($rates->count() === 0) { Log::error("Didn't find any currency rates for displaying!"); abort(503, 'Currency rates unavailable!'); } $latest_update = CurrencyRate::getMaxForDate(); return view('index', ['rates' => $rates, 'latest_update' => $latest_update]); }
private function createOrUpdate($rates) { foreach ($rates as $r) { $date = Carbon::createFromFormat('Y-m-d', $r->date); $start_of_day = $date->startOfDay(); $rate = Currency::where('currency', $r->currency)->first()->rates()->forDate($start_of_day)->first(); if (!$rate) { $currency = Currency::where('currency', $r->currency)->first(); CurrencyRate::create(['currency_id' => $currency->id, 'rate' => $r->rate, 'for_date' => $start_of_day]); } else { $rate->rate = $r->rate; $rate->save(); } } }