public function retrieve(Request $request, Weather $weather) { if (!$request->has('lat') || !$request->has('lon')) { return response('Please provide a lat and lon', 400); } $input = $request->all(); // We grab the lat and lon $lat = $input['lat']; $lon = $input['lon']; // We grab the latest data from this lat and long $rawData = file_get_contents('http://api.openweathermap.org/data/2.5/weather?lat=' . $lat . '&lon=' . $lon); if (!$rawData) { // We failed to retrieve data from the webservice // Just return the stuff we have return $weather->where('lat', $lat)->where('lon', $lon)->limit(10)->get(); } $jsonData = json_decode($rawData, true); // We transform this data $data = ['dt' => $jsonData['dt'], 'lat' => $lat, 'lon' => $lon, 'type' => $jsonData['weather'][0]['main'], 'temp' => $jsonData['main']['temp'] - 273.15]; // Check if we already have a record with same lat/lon and dt in our database $weatherCheck = $weather->where('lat', $lat)->where('lon', $lon)->where('dt', Carbon::createFromTimeStamp($data['dt']))->limit(1)->get(); // Record isn't in our db yet if ($weatherCheck->isEmpty()) { // We store the data in our database $weather->create($data); } // We grab the last 10 weather report from given lat/lon and return it return $weather->where('lat', $lat)->where('lon', $lon)->limit(10)->get(); }
/** * Display a listing of the resource. * * @return Response */ public function getWeather() { $key = env('FORECAST_IO_API_KEY'); $forecast = new Forecast($key); $weather = $forecast->get('34.101655', '-117.707591'); for ($i = 0; $i < 7; $i++) { if ($i == 0) { $current = $weather->currently->temperature; } else { $current = -1; } $date = Carbon::createFromTimeStamp($weather->daily->data[$i]->time)->toDateTimeString(); $dateId = substr($date, 0, 10); $id = DB::table('email_articles')->where('post_date', $dateId)->value('article_id'); $icon = $weather->daily->data[$i]->icon; if (strpos($icon, 'night') !== false) { $icon = 'clear-day'; } $max = $weather->daily->data[$i]->temperatureMax; $min = $weather->daily->data[$i]->temperatureMin; $sunset = Carbon::createFromTimeStamp($weather->daily->data[$i]->sunsetTime)->toDateTimeString(); $sunrise = Carbon::createFromTimeStamp($weather->daily->data[$i]->sunriseTime)->toDateTimeString(); echo strpos($icon, 'night'); if (Weather::where('article_id', '=', $id)->exists()) { DB::table('weather')->where('article_id', $id)->update(array('icon' => $icon, 'current_temp' => $current, 'max' => $max, 'min' => $min, 'sunriseTime' => $sunrise, 'sunsetTime' => $sunset, 'updated_at' => Carbon::now())); echo "Updated " . $date; } else { $entry = new Weather(); $entry->article_id = $id; $entry->icon = $icon; $entry->current_temp = $current; $entry->max = $max; $entry->min = $min; $entry->sunsetTime = $sunset; $entry->sunriseTime = $sunrise; $entry->save(); echo "Stored " . $date . "!"; } } }
public function ListareDetaliiHotel($tara, $oras, $slughotel) { $hotel = Hoteluri::findBySlug($slughotel); $oras = Orase::findbyslug($oras); $hotel_id = Hoteluri::findOrFail($hotel->id); $weather = Weather::where('HotelID', $hotel->id)->get(); $hoteluriSimilare = Hoteluri::where('AidaID', '<>', $hotel->AidaID)->where('City', $oras->AidaID)->take(3)->get(); return view('frontend.detalii-hotel')->with('hotel', $hotel)->with('weather', $weather)->with('hoteluriSimilare', $hoteluriSimilare)->with('hotel_id', $hotel_id); }