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(); } } }