public function history(\Illuminate\Http\Request $request, $days = 30) { for ($i = 0; $i < $days; $i++) { $day = Carbon::now()->subDays($i); $service_provider = get_called_class(); $data = $service_provider::getSourceDataByDate($day->format('j.n.Y')); foreach ($data as $code => $course) { $currency = Currency::where('code', $code)->first(); if ($currency) { CurrenciesHistory::create(['currency_id' => $currency->id, 'rate' => $course['rate'], 'created_at' => $day->format('Y-m-d H:i:s')]); } } } if ($request->ajax()) { return ['success' => true]; } return redirect()->back()->withMessage(); }
/** * {@inheritDoc} */ public function updated(Currency $currency) { // Currency rate changed CurrenciesHistory::create(['rate' => $currency->unit, 'currency_id' => $currency->id]); $this->flushCache($currency); }
public function history($days = 30) { $histories = []; $day = 24 * 60 * 60; $now = Carbon::now()->timestamp; $start = $now - $days * $day; foreach (Currency::where('unit', '!=', '1')->get() as $currency) { $values = []; for ($i = 0; $i < $days; $i++) { $day_before = $start + $i * $day; $day_after = $start + ($i + 1) * $day; $history = CurrenciesHistory::where('currency_id', $currency->id)->where('created_at', '>', \Carbon\Carbon::createFromTimeStamp($day_before)->format('Y-m-d H:i:s'))->where('created_at', '<', \Carbon\Carbon::createFromTimeStamp($day_after)->format('Y-m-d H:i:s'))->first(); if ($history) { $values[] = [$day_before, $history->rate]; } else { $values[] = [$day_before, 1]; } } $histories[] = ['key' => $currency->name, 'values' => $values]; } return $histories; }