/** * save array of data as objects in db */ private function save($result, $type) { // loop foreach ($result as $s) { $saved = false; if (isset($s['sid'])) { // pull $saved = ServiceStop::where('tid', $s['tid'])->where('sid', $s['sid'])->where('date', $s['date'])->first(); } if ($saved) { // update fields foreach ($s as $key => $value) { $saved->{$key} = $value; } // save to db $saved->save(); } else { // insert ServiceStop::insert($s); } } }
/** * Get all trips. * * @return array */ private function get_trips() { // variables $cache_key = 'trips'; $cache_time = 10; // minutes $date_delay = 3; // hours // try cache if (Cache::has($cache_key)) { return Cache::get($cache_key); } // cache fail // get all trip id's $result = ServiceStop::where('date', (int) date('Ymd', strtotime("- {$date_delay} hours")))->whereNotNull('sequence')->distinct('tid')->get()->toArray(); // build assoc $hash = array(); // loop foreach ($result as $s) { $hash[$s[0]] = array(); } // get all trips $fields = array('tid', 'date', 'sequence', 'arrival_time', 'arrival_delay', 'departure_time', 'cancelled'); $result = ServiceStop::where('date', (int) date('Ymd', strtotime("- {$date_delay} hours")))->get($fields)->toArray(); // build hash foreach ($result as $s) { // safety if (!isset($s['sequence'])) { continue; } // indexed $hash[$s['tid']][$s['sequence']] = $s; } // cache it Cache::put($cache_key, $hash, $cache_time); // and return return $hash; }